在目录中找到与grep不匹配的行

时间:2016-06-13 08:59:30

标签: bash shell

我有这个bash脚本:

function getlist() {
       grep -E 'pattern' ../fileWithInput.js | sed "s#^regexPattern#\1 \2#" | grep -v :
   }

  getlist | while read line; do
  method=$(echo $line | awk '{ print $1 }')
  uri=$(echo $line | awk '{ print $2 }')
 `grep "$method" -vr  .
  #echo method: $method uri: $uri
  done

问题: 目前我有很多'模式'字符串。如何检查目录和仅输出'模式'不匹配的字符串。

我在fileWithInput.js中有什么示例:

'foo','bar','hello'.

〜/回购/ anotherDirectory:

'foo','bar'.

如何只打印不在/ repo / anotherDirectory中的fileWithInput.js中的字符串? 最终输出必须是这样的:

'hello': 0 matches.

请帮助grep命令执行此操作。或许你有另一个想法。感谢您的关注,祝您度过愉快的一天!

1 个答案:

答案 0 :(得分:1)

FILE1.TXT

'foo','bar','hello'.

filem.txt

'foo','bar'.

使用awk

  awk 'BEGIN{RS="[,\\.]"} NR==FNR{a[$0];next} {delete a[$0]} END{for(i in a){print i": 0 matches."}} ' filei.txt  filem.txt

代码细分:

BEGIN{RS="[,\\.]"} # Record seperator , or .
NR==FNR{a[$0];next} # store values ina array a and skip from next process
{delete a[$0]} # delete from array if file1 exists in file2
END{
for(i in a){
    print i": 0 matches."} # print missing items
} 

输出:

'hello': 0 matches.