[英]how to create same line if else statement around wc -l output
I'm not at all familiar with tcsh, so really hoping someone could drop some knowledge on what i'm missing here. 我对tcsh一点都不熟悉,所以我真的希望有人可以对我在这里缺少的东西有所了解。 I'm trying to build a single lined if else statement that checks the number of rows in a file, and outputs a touch file if the number of rows > 1. I'm checking if rows is greater than 1 because the file can be generated with just header info.
我正在尝试构建单行的if else语句,该语句检查文件中的行数,如果行数> 1,则输出触摸文件。我正在检查行数是否大于1,因为文件可以仅使用标题信息生成。 (Separately if anyone has any useful online guides that I may use for all things tcsh, that would also be very helpful)
(另外,如果任何人有任何有用的在线指南,我可以将其用于所有tcsh,那也将非常有帮助)
Example file 1 (has data): 示例文件1(具有数据):
title1,title2 data1,data2 title1,title2 data1,data2
Example file 2 (empty): 示例文件2(空):
title1,title2 TITLE1,标题2
Expected output: 预期产量:
If file has data, generates a has_data.txt, else does not generate file 如果文件包含数据,则生成has_data.txt,否则不生成文件
What I've tried: 我尝试过的
wc -l dummy_file_empty.txt >1 && touch has_data.txt - does not really evaluate greater than wc -l dummy_file_empty.txt> 1 &&触摸has_data.txt- 实际评估结果不大于
update `` is used for evaluating statements in line, and $output is an array? 更新 ``用于在线路评价语句,$输出是一个数组? So needed to reference index 1 to get the line count
所以需要参考索引1来获得行数
set output=`wc -l dummy_file_empty.txt` | 设置output =`wc -l dummy_file_empty.txt` | if($output[1] >1) touch has_data.txt - returns expression syntax error
if($ output [1]> 1)touch has_data.txt- 返回表达式语法错误
if ( wc -l dummy_file_empty.txt ) > 10 touch has_data.txt - returns expression syntax error 如果(wc -l dummy_file_empty.txt)> 10 touch has_data.txt- 返回表达式语法错误
i can invoke bash shell as-well if a solution exists in bash ( saw examples where bash is piped at the end of statement |bash and tested it to ensure it works on my machine) 如果bash中存在解决方案,我也可以调用bash shell(请参见示例,其中bash在语句bash的末尾通过管道传输,并对其进行了测试以确保其在我的机器上可以工作)
[[ $(cat file.txt | wc -l) -gt 1 ]] && touch has_data.txt
When you use >
this will attempt to write to a file. 当您使用
>
这将尝试写入文件。 In this case a file named 1
or 10
. 在这种情况下,文件名为
1
或10
。 You want to use -gt
which stands for greater than. 您要使用
-gt
代表大于。
If you're running this in a tcsh
terminal, then you can invoke this via bash: 如果您正在
tcsh
终端中运行此命令,则可以通过bash调用此命令:
bash -c '[[ $(cat file.txt | wc -l) -gt 1 ]] && touch has_data.txt'
Just wanted to add a solution where I've gotten somewhat close..albeit i'm not really able to handle scenarios where the file might not exist. 只是想在距离我有点近的地方添加一个解决方案。尽管我实际上无法处理文件可能不存在的情况。 Hopefully somebody has guidance on whether this a good way to do it.
希望有人对这是否是一个好方法有指导。
set output=`wc -l file.txt` && if ( $output[1] > 1 ) touch has_data.txt 设置输出=`wc -l file.txt` &&如果($ output [1]> 1)触摸has_data.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.