[英]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.