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

Have a good regex question needed to modify some files. Suppose you have a tab deliminated log file that you want to save only those lines which contain a particular piece of text, and delete the rest of the lines. Each new row entry is determined by a new line. So for example:

1: 2013-06-01 09:20:43 /stuff_I_want 200
2: 2013-06-01 09:20:43 /stuff_I_want 200
3: 2013-06-01 09:20:43 /stuff_I_dontwant 200
4: 2013-06-01 09:20:43 /stuff_I_want 200

So lets say I only want to keep lines 1,2,4, and delete 3. (The file doesn't have 1,2,3,4, I'm just denoting that line by line). How can I construct a regex to keep only what I want and delete all else from file? But remember, I want the ENTIRE line from the parameter of "stuff_I_want" in the regex. Thanks!

Replies are listed 'Best First'.
Re: RegEx Question!
by hbm (Hermit) on Jun 07, 2013 at 21:04 UTC

    Don't think of it as "deleting". Rather, matching only what you want. Perhaps writing the matches to a new file; and perhaps then overwriting the original file.

    perl -lne 'print unless /dontwant/' log > log.new
Re: RegEx Question!
by CountZero (Bishop) on Jun 08, 2013 at 07:41 UTC
    It will all depend on the data (whether or not to discard what you don't want or keep what you want), but you can use this as a basis for a "keep it" script:
    perl -lne 'print if /stuff_I_want/' log > log.new
    (With thanks to hbm)

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Absolutely -lne is the right way to go about this, we can even add an or options for additional parameters to check for. Thank you
Re: RegEx Question!
by Laurent_R (Canon) on Jun 08, 2013 at 11:22 UTC

    You can also edit "in place" so that it actually looks as deleting the lines you don't want from the original file. Modifying HBM's example, this could look like this:

    perl -i.bak -ne 'print unless /dontwant/' my_file.txt

    There, unwanted lines will be removed from my_file.txt and the original version of the file will be saved with the .bak extension added.