b_vulnerability has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody!
I have a problem with a text file that I need to clean up.
My text file is like this:
ironia/ironia/S-FS  (/(/PU  lo/lo/PQMS  soprannomineranno/soprannominare/V-P3IF  "/"/PU  Pique/pique/SP  la/lo/RDFS  lune/luna/S-FP  "/"/PU  a/a/E  causa/causa/S-FS  della/di/E-FS  forma/forma/S-FS  del/di/E-MS  suo/suo/A-MS  naso/naso/S-MS
I have to remove everything that follows "-" so I can get:
ironia/ironia/S  (/(/PU  lo/lo/PQMS  soprannomineranno/soprannominare/V  "/"/PU  Pique/pique/SP  la/lo/RDFS  lune/luna/S  "/"/PU  a/a/E  causa/causa/S della/di/E  forma/forma/S  del/di/E  suo/suo/A naso/naso/S
I've tried this regex:
 s/-\w{2,}/ /;
but I don't know how to make ti work, beacuse this leaves everything the way it was.
I'm sorry if this is a dumb question, but I really don't know how to do that.
Thanks for you help

Replies are listed 'Best First'.
Re: Deleting text from a file using regex
by ikegami (Patriarch) on Nov 10, 2008 at 10:38 UTC

    You need the "g" modifier to replace all instances.

    perl -pe"s/-\w{2,}/ /g" infile > outfile

    Or in-place:

    perl -i.bak -pe"s/-\w{2,}/ /g" file

    Or in code

    while (<$fh_in>) { s/-\w{2,}/ /g; print $fh_out $_; }
      I forgot the g modifier, apparently, because now my regex works just fine! Thanks, really!
Re: Deleting text from a file using regex
by moritz (Cardinal) on Nov 10, 2008 at 10:39 UTC
    Your regex works for me:
    $ cat foo ironia/ironia/S-FS (/(/PU lo/lo/PQMS soprannomineranno/soprannomina +re/V-P3IF "/"/PU Pique/pique/SP la/lo/RDFS lune/luna/S-FP "/"/PU + a/a/E causa/causa/S-FS della/di/E-FS forma/forma/S-FS del/di/E- +MS suo/suo/A-MS naso/naso/S-MS $ cat foo | perl -pe "s/-\w{2,}//; " ironia/ironia/S (/(/PU lo/lo/PQMS soprannomineranno/soprannominare/ +V-P3IF "/"/PU Pique/pique/SP la/lo/RDFS lune/luna/S-FP "/"/PU a +/a/E causa/causa/S-FS della/di/E-FS forma/forma/S-FS del/di/E-MS + suo/suo/A-MS naso/naso/S-MS

    If you want to modify the file in-place, take a look at the -i switch

    Update: I didn't notice that you want to delete all occurrences. ikegami++

Re: Deleting text from a file using regex
by jwkrahn (Abbot) on Nov 10, 2008 at 10:39 UTC
    $ echo 'ironia/ironia/S-FS (/(/PU lo/lo/PQMS soprannomineranno/sopr +annominare/V-P3IF "/"/PU Pique/pique/SP la/lo/RDFS lune/luna/S-FP + "/"/PU a/a/E causa/causa/S-FS della/di/E-FS forma/forma/S-FS d +el/di/E-MS suo/suo/A-MS naso/naso/S-MS' | perl -lpe's/-\S+//g' ironia/ironia/S (/(/PU lo/lo/PQMS soprannomineranno/soprannominare/ +V "/"/PU Pique/pique/SP la/lo/RDFS lune/luna/S "/"/PU a/a/E ca +usa/causa/S della/di/E forma/forma/S del/di/E suo/suo/A naso/nas +o/S