2

The following command gives me a list of matching expressions:

grep -f /tmp/list Filename* > /tmp/output

The list file is then parsed and used to search Filename* for the parsed string. The results are then saved to output.

How would I output the parsed string from list in the case where there is no match in Filename*?

Contents of the list file could be:

ABC
BLA
ZZZ
HJK

Example Files:

Filename1:5,ABC,123
Filename2:5,ZZZ,342

Result of Running Command:

BLA
HJK

Stack overflow question 2480584 looks like it may be relevant, through the use of an if statement. However I'm not sure how to output the parsed string to the output file. Would require some type of read line?

TIA,

Mic

4

2 回答 2

1

Obviously, grep -f list Filename* gives all matches of patterns from the file list in the files specified by Filename*, i.e.,

Filename1:5,ABC,123
Filename2:5,ZZZ,342

in your example.

By adding the -o (only print matching expression) and -h (do not print filename) flags, we can turn this into:

ABC
ZZZ

Now you want all patterns from list that are not contained in this list, which can be achieved by

grep -f list Filename* -o -h | grep -f /dev/stdin -v list

where the second grep takes it's patterns from the output of the first and by using the -v flag gives all the lines of file list that do not match those patterns.

于 2013-08-09T10:24:29.757 回答
0

This makes it:

$ grep -v "$(cat Filename* | cut -d, -f2)" /tmp/list
BLA
HJK

Explanation

$ cat Filename* | cut -d, -f2
ABC
ZZZ

And then grep -v looks for the inverse matching.

于 2013-08-09T09:51:32.147 回答