Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个运行良好的 awk 命令:
awk '$1==1{print $1,$2,$4}' file > out1
但是,如果我将相同的 awk 命令放在 for 循环中,我只会得到 emtpy 输出文件:
for i in {1..22} do awk '$1==$i{print $1,$2,$4}' file > out$i done
这给出了 22 个空输出文件。for循环有什么问题?
尝试:
for i in {1..22} do awk -v num="$i" '$1==num{print $1,$2,$4}' file > out$i done
试试这一行:
awk -vvar="$i" '$1==var{print $1,$2,$4}' file >out$i
实际上你不需要那个 for 循环,如果你的 $1 总是一个整数,也试试这个:
awk '$1>0&&$1<=22{print $1,$2,$4>"out"$1}' file