views:

64

answers:

4

I have a linux filter to extract all lines from an xcode project that contain localized strings and produce 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 strings looking like this

@"string1"
@"string2"
etc

What I now need to do is identify the entries that do not exist within another textfile. So imagine I have a text file containing;

@"string1"
@"string3"
etc

The result would be @"string2" as it is not present in the file

For the sake of argument, the file is named list.txt

What do I need to add to my filter? I'm sure I can do this with grep but my brain has failed!

+1  A: 

You could use comm:

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

Also - you can probably omit the uniq and use sort -u if it's available.

martin clayton
+2  A: 

You can do:

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

You pipe the output of perl to grep which is using -v option to invert the search and -f option to get the search pattern from file

codaddict
Thanks! That's helped me with another issue - I have realised I have asked the wrong question, I'm going to create a new one as what I actually need is different - apologies!
Roger
A: 

It might be worth making a script out of this (not tested):

#!/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";

Invoke it using:

$ find_missing list.txt
Sinan Ünür
Didn't want to write a script to do it - I could do that easily enough but wanted to see how to do it without that.
Roger
A: 

a Simple GREP switch (-v) prints the inverse. So the command would be

GREP -v -f filename1 filename2 > filename3

Konark Modi