如何在while循环内创建if语句以仅在某些行上执行某些操作?

时间:2019-05-18 12:05:59

标签: bash

我正在尝试编写一个在sh中启动的简单bash脚本,该脚本允许我从输入文件开始创建一个新的输出文件,并保持每行以“>”开头,而对于每行满足此要求,它必须删除第三个字符,然后将其挂在新文件中。

输入文件:

>0197_16S  
-AAAAACATGTCCTCTTGTTTATA-----TNTGAGGTTTGACCTGCCCTATG--A---  
>0688_16S    
-----ACATCTTCTCTTGAGTTAT-----TTTGAGATATGACCTGCCCAATG--A-T-  
.  
.  
.  
.  

sh脚本:

while IFS= read line; do  
if [ "$line" = ">"* ]; then echo "$line" >> output.txt  
else
var=$(echo "$line" | awk -vFS= '{for (i = 1; i <=NF; i+3) {printf $i(i+1)} printf "\n"}');  
echo "$var" >> output.txt  
fi;  
done <foo.txt  

else语句似乎有效,但是if的条件从未得到验证,因此也从字符“>”开头的行中删除了每个第三个字符。

实际输出:

>09716  
-AAACAGTCTTTTTAT----NTAGTTGACTCCTAG-A--  
>08816
----CACTCTTTAGTA----TTAGTAGACTCCAAG-A--  
.  
.  
.  

预期输出:

>0197_16S  
-AAACAGTCTTTTTAT----NTAGTTGACTCCTAG-A--  
>0688_16S  
----CACTCTTTAGTA----TTAGTAGACTCCAAG-A--  
.  
.  
.  

1 个答案:

答案 0 :(得分:0)

尝试避免while循环。

没有条件NameError Traceback (most recent call last) <ipython-input-47-2f2a81626e49> in <module> 35 # Initial condition 36 y0 = T1, t1 ---> 37 sol = odeint(funct, y0 , x) 38 39 # plot c:\users\idril\appdata\local\programs\python\python36\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst) 242 full_output, rtol, atol, tcrit, h0, hmax, hmin, 243 ixpr, mxstep, mxhnil, mxordn, mxords, --> 244 int(bool(tfirst))) 245 if output[-1] < 0: 246 warning_msg = _msgs[output[-1]] + " Run with full_output = 1 to get quantitative information." <ipython-input-47-2f2a81626e49> in funct(y, x) 23 24 def funct(y, x): ---> 25 y = T, t 26 dTdx = (U * Di * pi / (Wt * cpt)) * (T - t) 27 dtdx = (U * Di * pi / (Ww * cpw)) * (T - t) NameError: name 'T' is not defined 的情况下您可以

keeping each line starting with ">" in its first position

通过更改所有不匹配的行,可以为带有sed -r 's/(..)./\1/g' foo.txt 的行添加条件

>

或使用sed -r '/^>/ !s/(..)./\1/g' foo.txt

awk