【发布时间】:2021-06-11 13:45:47
【问题描述】:
我正在尝试编写一个脚本来读取文件的所有行,如果在 grep 中包含 expecify 单词(在本例中为 apple),则 grep 一行,但我遇到了 grep 忽略的问题/没有捕捉到文件的第一行。
这是我的脚本:
#!/bin/bash
while read line;
do
grep 'apple' > fruits2.txt
done < fruits.txt
输入文件“fruits.txt”
apple 1
banana
apple 2
grape
orange
apple 3
blueberry
apple 4
输出文件“fruits2.txt”
apple 2
apple 3
apple 4
【问题讨论】:
-
read line读取第一行,grep 甚至看不到它。 -
只需使用
grep 'apple' fruits.txt > fruits2.txt,无需循环。 -
请将该示例输入的所需输出(无描述、无图像、无链接)添加到您的问题(无评论)。