Bash 循环遍历文件末尾
我正在编写一个脚本,该脚本将从一个关键字和一个单独文件上的其他关键字列表中查找模式。
File1 包含列表,每行一个单词。 File2 还有另一个列表——我真正想要搜索的列表。
while read LINE; do
grep -q $LINE file2
if [ $? -eq 0 ]; then
echo "Found $LINE in file2."
grep $LINE file2 | grep example
if [ $? -eq 0 ]; then
echo "Keeping $LINE"
else
echo "Deleting $LINE"
sed -i "/$LINE/d" file2
fi
else
echo "Did not find $LINE in file2."
fi
done < file1
我想要的是从 file1 中取出每个单词并在 file2 中搜索它的每个实例。我想从这些实例中搜索包含单词 example 的所有实例。任何不包含示例的实例,我想删除它们。
我的代码,它从 file1 中获取一个单词并在 file2 中搜索它的实例。一旦找到该实例,循环就会转到 file1 中的下一个单词,此时它应该继续在 file2 中搜索前一个单词;当它完成在 file2 中搜索当前单词时,它应该只移动到下一个 file1 单词。
对如何实现这一目标有任何帮助吗?
I'm working on script that will find a pattern from a keyword and a list of other keywords on a separate file.
File1 has the list, which is a word per line. File2 has another list--the one that I actually want to search.
while read LINE; do
grep -q $LINE file2
if [ $? -eq 0 ]; then
echo "Found $LINE in file2."
grep $LINE file2 | grep example
if [ $? -eq 0 ]; then
echo "Keeping $LINE"
else
echo "Deleting $LINE"
sed -i "/$LINE/d" file2
fi
else
echo "Did not find $LINE in file2."
fi
done < file1
What I want is to take each word from file1 and search for every instance of it in file2. From those instances, I want to search for all the instances that contain the word example. Any instances that dont contain example, I want to delete them.
My code, it takes a word from file1 and searches for an instance of it in file2. Once it finds that instance, the loop moves on to the next word in file1, when it should continue searching for file2 for the previous word; it should only move on to the next file1 word when it has completed searching file2 for the current word.
Any help on how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
建议使用
awk
脚本,仅扫描每个文件一次。Suggesting
awk
script, to scan each file only once.