-2

I am trying to edit some files using regular expressions.

sed seemed too limited so I switched to Perl.

There might be something wrong in my syntax.

The regex pattern works in TextMate.

Nothing happens when I use Perl.

I want to remove all lines not containing SourceFile or "".

For your information, it's for cleaning up output data from exiftools.

$ perl -pe 's/^(?!.*(SourceFile|"").*).*$\n//g' out.csv

$ cat out.csv
SourceFile,GPSLatitude,GPSLongitude
1.jpg,,
2.jpg,,
3.jpg,"10 deg 20' 30.30"" N","40 deg 50' 60.60"" W"
4.jpg,"10 deg 20' 30.30"" N","40 deg 50' 60.60"" W"

The clean up should keep the first line and all lines containing GPS data. So after clean up it should look like:

SourceFile,GPSLatitude,GPSLongitude
3.jpg,"10 deg 20' 30.30"" N","40 deg 50' 60.60"" W"
4.jpg,"10 deg 20' 30.30"" N","40 deg 50' 60.60"" W"
Kasam
  • 29
  • 5
  • 1
    What are you trying to do with this regex? Include your input & output samples. – anubhava Oct 13 '16 at 21:38
  • One option, if you can rerun your exiftool command again, would be to tell exiftool to only process files that contained gps coordinates e.g. `exiftool -if '$GPSLatitude and $GPSLongitude' -GPSLatitude -GPSLongitude -csv >out.csv TargetDir` – StarGeek Oct 14 '16 at 03:24
  • Thanks @StarGeek ! Most useful help. – Kasam Oct 14 '16 at 21:52

1 Answers1

1

If you want to remove a line, you don't need to make a replacement. Check the condition and display the line or not:

[edit] It seems you want the opposite that your initial pattern suggests:

perl:

perl -ne '/SourceFile|""/&&print' file

awk:

awk '/SourceFile|""/' file

grep:

grep 'SourceFile\|""' file

[old answer]

perl -ne '!/SourceFile|""/&&print' file

Shorter with awk:

awk '!/SourceFile|""/' file

or grep:

grep -v 'SourceFile\|""' file
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125