如何在linux命令上组合if语句和sort



我想使用包含.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之前的输出为:

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

我希望我的输出像:

如果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

所以我的问题是,我不知道如果语句和排序一起连接到谁。

不需要使用awk。将perl脚本的输出通过管道传输到shell块,该块读取第一行,测试它是正的还是负的,然后调用适当的排序。

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

最新更新