我正在通过子集设置文件名grep,然后将生成的文件与cat. 但是,对于如何使用 for 语句,我仍然有些困惑,例如for ((i=1;i<23;i+=1));
鉴于我的文件file1.txt,我想 grepsample1如下:
grep -w '^sample1' file1.txt > sample1_file.txt
grep -w '^sample2' file2.txt > sample2_file.txt
grep -w '^sample3' file3.txt > sample3_file.txt
....
grep -w '^sample22' file22.txt > sample22_file.txt
Run Code Online (Sandbox Code Playgroud)
然后连接这些:
cat sample1_file.txt sample2_file.txt sample3_file.txt ... sample22_file.txt > final_output.txt
Run Code Online (Sandbox Code Playgroud)
尝试:
for i in {1..22}
do
grep -w "^sample$i" "file$i.txt"
done >final_output.txt
Run Code Online (Sandbox Code Playgroud)
{1..22} 遍历从 1 到 22 的所有整数。 对于不熟悉 C 的人来说,它可能比 C 更直观(但不太灵活) ((i=1;i<23;i+=1))
将表达式^sample$i放在双引号内而不是单引号内很重要,这样 shell 将展开$i。
如果您只需要final_output.txt,则无需创建中间文件。
请注意,将重定向final_output.txt放在done语句之后是有效 的:这样,shell 只需打开和关闭此文件一次。
| 归档时间: |
|
| 查看次数: |
217 次 |
| 最近记录: |