【发布时间】:2020-12-08 10:20:42
【问题描述】:
---- 我的文本文件,我必须从中搜索关键字 [文件名 --- 测试]
centos is my bro$
red hat is my course$
ubuntu is my OS$
fqdn is stupid $
$
$
$
tom outsmart jerry$
red hat is my boy$
jerry is samall
------关键字文件为[word.txt]
red hat$
we$
hello$
bye$
Compensation
-----我的代码
while read "p"; do
paste -d',' <(echo -n "$p" ) <(echo "searchall") <( grep -i "$p" test | wc -l) <(grep -i -A 1 -B 1 "$p" test )
done <word.txt
----我的期望,输出应该是
keyword,serchall,frequency,line above it
line it find keyword in
line below it
red hat,searchall,2,centos is my bro
red hat is my course
ubuntu is my OS
red hat,searchall,2,tom outsmart jerry
red hat is my boy
jerry is samall
---- 但来自我的代码的输出
red hat,searchall,2,centos is my bro
,,,red hat is my course
,,,ubuntu is my OS
,,,--
,,,tom outsmart jerry
,,,red hat is my boy
,,,jerry is samall
----请给我建议并指出正确的方向以获得所需的输出。
---- 我正在尝试从文件中 grep 关键字并打印它们 这里应该创建两条记录,因为关键字(red hat)来了两次
----如何循环访问关键字的出现频率。
【问题讨论】:
-
如果
red hat出现在输入文件的两行中,输出会是什么?如果它根本没有出现怎么办?如果red hate存在于您的输入文件中,它是否应该与您的“关键字”文件中的red hat匹配?换句话说,您是要进行部分匹配还是完全匹配?另外,您是否尝试正则表达式或文字字符串匹配? -
@EdMorton 是的,先生,您是对的,在我的输入文件中,关键字在 2 行中背靠背出现,这是一个问题。第二,我是编码新手和 bash,您可以提供 cmets 或参考给出代码,以便我能理解它是如何工作的先生
-
那没有回答我的问题。请尝试自己弄清楚我的代码,因为它非常简单(阅读手册页,添加打印语句以查看哪些变量具有哪些值等),如果您有任何具体问题,请在我的回答下提问。
标签: linux bash shell loops grep