Filter with grep to print when text is NOT present in another file

I have a linux filter to extract all the lines from an xcode project containing localized lines and create a sorted list of unique entries. The filter works fine and is shown below.

grep NSLocalized *.m | perl -pe 's/.*NSLocalizedString\((.+?)\,.*/$1/' | sort | uniq 

The result is a list of lines similar to

 @"string1" @"string2" etc 

Now I need to define entries that do not exist in another text file. So imagine that I have a text file containing:

 @"string1" @"string3" etc 

The result will be @"string2" because it is not in the file

For the argument, the file is named list.txt

What do I need to add to my filter? I'm sure I can do it with grep, but my brain will not work!

+4
source share
4 answers

You can do:

 grep NSLocalized *.m | perl -pe 's/.NSLocalizedString((.+?)\,./$1/' | grep -v -f list.txt | #ONLY ADDITION sort | uniq 

You pass the perl output to grep , which uses the -v option to invert the search and -f to get the search pattern from the file

+4
source

A simple GREP switch (-v) prints the reverse. So the team will

GREP -v -f filename1 filename2> filename3

+5
source

You can use comm :

 ... your pipeline | comm -23 - list.txt 

Alternatively, you can possibly omit uniq and use sort -u if available.

+2
source

It might be worth making a script from this (not verified):

 #!/usr/bin/perl use strict; use warnings; my %existing; while ( <> ) { chomp; $existing{ $_ } = 1; } my %nonexisting; while ( defined( my $file = glob '*.m') ) { open my $h, '<', $file or die "Cannot open '$file': $!"; while ( my $line = <$h> ) { if ( my ($string) = $line =~ /NSLocalizedString\((.+?),/ ) { $existing{ $string } or $nonexisting{ $string } = 1; } } } print join("\n", sort keys %nonexisting), "\n"; 

Call it using:

  $ find_missing list.txt 
0
source

All Articles