bash的搜索字符串中的文件的每一行 [英] bash search for string in each line of file
本文介绍了bash的搜索字符串中的文件的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想什么似乎是一个非常简单的任务:使用bash的搜索字符串的文件,如果存在的话,这些输出到另一个文件。这可能是时差,但这应该工作:
I'm trying what seems like a very simple task: use bash to search a file for strings, and if they exist, output those to another file. It could be jetlag, but this should work:
#!/bin/bash
cnty=CNTRY
for line in $(cat wheatvrice.csv); do
if [[ $line = *$cnty* ]]
then
echo $line >> wr_imp.csv
fi
done
我也试过这样的完整性:
I also tried this for completeness:
#!/bin/bash
cnty=CNTRY
for line in $(cat wheatvrice.csv); do
case $line in
*"$cnty"*) echo $line >> wr_imp.csv;;
*) echo "no";;
esac
done
两个输出的一切,不管行是否包含CNTRY或没有,我从看似可靠的消息来源复制/粘贴,这样显然有关于我的思念庆典岬简单的东西?
both output everything, regardless of whether the line contains CNTRY or not, and I'm copy/pasting from seemingly reliable sources, so apparently there's something simple about bash-ness that I'm missing?
推荐答案
不要使用bash,使用grep。
Don't use bash, use grep.
grep -F "$cnty" wheatvrice.csv >> wr_imp.csv
这篇关于bash的搜索字符串中的文件的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文