繁体   English   中英

如何在 linux 命令上组合 if 语句和排序?

[英]How to combine if statement and sort on linux command?

我想使用命令行对包含 .coordinates.txt 文件的文件夹多次运行 Perl 脚本,执行多个“操作”,最后一步,我想根据第一行值进行排序。 我写了这个:

 for i in ./*gb.coordinates.txt; do perl myscript $i | 
        awk 'NR==1 {print $2,"\t***here"; next } 1'|sed '2d'| #the output has an empty line in the second row so I remove it and I add "\t***here" to have and idea about the first line value after my final sorting

        if [[awk 'FNR == 1 && $1>0']] then {sort -k1nr} else {sort -k1n} fi
         > $i.allvalues.txt;
   done

直到这里:

for i in ./*gb.coordinates.txt; do perl myscript $i | awk 'NR==1 {print $2,"\t***here"; next } 1'|sed '2d' > $i.allvalues.txt; done

一切正常。

所以正如我在上面写的我想要获得的最后一步是这样的:

 if the first line of my output >=0 then sort -k1n else sort -k1nr

if condition前的output为:

XXXX   eiter positive number or negative \t***here
32
4455
-2333
23
-123

我希望我的 output 像:

如果 xxxx= 正

xxxx (going in the correct order)  \t***here
4455
32
23
-123
-2333

如果 xxxx= 负

xxxx (going in the correct order)   \t***here 
-2333
-123
23
32
4455

所以我的问题是我不知道将 if 语句与 sort 连接起来。

无需使用awk Pipe the output of the perl script to a shell block that reads the first line, tests whether it's positive or negative, and then calls the appropriate sort.

for i in ./*gb.coordinates.txt; do 
    perl myscript $i | {
        read _ firstline __
        if (( firstline > 0 ))
        then sort -k1nr
        else sort -k1n
        fi
    } > $i.allvalues.txt
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM