Jim*_*Jim 2 command-line osx grep pipe sed
我有2个文件:
$ cat file1
jim.smith
john.doe
bill.johnson
alex.smith
$ cat file2
"1/26/2017 8:02:01 PM",Valid customer,jim.smith,NY,1485457321
"1/30/2017 11:09:36 AM",New customer,tim.jones,CO,1485770976
"1/30/2017 11:14:03 AM",New customer,john.doe,CA,1485771243
"1/30/2017 11:13:53 AM",New customer,bill.smith,CA,1485771233
Run Code Online (Sandbox Code Playgroud)
我想从文件 2 中获取文件 1 中不存在的所有名称。
以下不起作用:
$ cut -d, -f 3 file2 | sed 's/"//g' | grep -v file1
jim.smith
tim.jones
john.doe
bill.smith
Run Code Online (Sandbox Code Playgroud)
为什么在这种情况下到 grep -v 的管道不起作用?
Kus*_*nda 11
这实际上是我对您之前问题的回答的最后一步。
您的解决方案的工作,如果你加-f在前面file1的grep:
$ cut -d, -f3 file2 | grep -v -f file1
tim.jones
bill.smith
Run Code Online (Sandbox Code Playgroud)
使用-f,grep将查找file1模式。没有它,它将简单地file1用作文字模式。
您可能还想使用-F,否则模式中的点将被解释为“任何字符”。当你在做的时候,也把它放在-x那里以使grep整个行执行匹配(如果你有一个joe.smith不应该匹配的将很有用joe.smiths):
$ cut -d, -f3 file2 | grep -v -F -x -f file1
Run Code Online (Sandbox Code Playgroud)
显然,这要求在行的末尾没有尾随空格file1(问题的文本中似乎有)。
请注意,sed不需要 ,因为 的输出cut不包含任何". 此外,如果您需要删除所有",那么tr -d '"'将是一个更好的工具。
尝试这个。没有sed。需要 GNUdiff和bash.
diff --new-line-format="" --unchanged-line-format="" <(cut -f3 -d, file2|sort) <(sort file1)
Run Code Online (Sandbox Code Playgroud)
产量结果:
bill.smith
tim.jones
Run Code Online (Sandbox Code Playgroud)