- 源文件内容
文件名 | 文件内容 |
file2_part.txt | name5 data15 dada16 name3 data9 dada10 data11 dada12 |
- for命令读取的行结果被空格符分开了
root@ae02635267dc:/workspace/autohome/test/shell/awk# for line in `cat file2_part.txt`;do echo $line;done;
name5
data15
dada16
name3
data9
dada10
data11
dada12
- while命令
root@ae02635267dc:/workspace/autohome/test/shell/awk# while read line; do echo $line;done <file2_part.txt
name5 data15 dada16
name3 data9 dada10 data11 dada12
- 应用场景:以文件B的行为待查找内容,然后从A文件中搜索指定数量的内容
while read line; do grep "$line" $A.txt|shuf|head -n 5;done <$B.txt >>rst.txt
参考文献: