【发布时间】:2012-11-23 14:41:51
【问题描述】:
我有一个文件,比如输入,包含如下模式:
quantum_mech_.*
astrophysics_.*
geology_.*
economy_*
我有另一个文件,比如主题,它看起来像:
quantum_mech_[101]
astrophysics_[102]
geology_[203]
quantum_mech_[007]
geology_[205]
我想 grep 输入文件中的每一行并搜索文件“主题”并仅输出第一个匹配项,如果在主题文件中根本找不到该行,则还打印“不匹配”。所以我期待这样的输出:
quantum_mech_[101]
astrophysics_[102]
geology_[203]
Not Matched
我知道这是一个很老的问题,但似乎没有一种方法对我来说正常工作。我尝试了以下代码的几种变体:
script.csh:
cat $1 | while read line
do grep $line ./subject | head -1 >> output
set VAR=$?
if ( $VAR==0 ) then
echo "Not Matched\n" >> output
endif
done
运行方式:
script.csh input
任何使用 sed/grep/csh 的帮助/指针都会很棒。
感谢和问候,
【问题讨论】:
-
你用的是什么外壳?
csh?您的 while 循环的csh语法错误。 -
是的,我正在使用 csh,我不是专家 :( 你能更正代码吗?谢谢
-
不要使用 csh 编写脚本。谷歌“csh 为什么不”。
标签: unix sed grep pattern-matching csh