MLUG: Re: [MLUG] perl question
Re: [MLUG] perl question
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Tue, 2 May 2006, Stephen Montgomery-Smith wrote:

Mike Miller wrote:
This must be something I can do in a one-liner: Suppose I have a file where I want to remove all occurances of a certain character from lines that match a pattern, like all 'x' characters from every line that begins with '1':

3 7 x 5 x 9
1 x 2 2 z x
9 9 9 x 9 9
1 2 23 x 7


perl -p -i -e 's/x//g if /^1/' the_file

(but I didn't test it)


I tested it and it works great.  Thanks, Stephen!
For those who don't know, these both overwrite the_file (the '-i' option):

perl -p -i -e 'stuff' the_file
perl -pi -e 'stuff' the_file

And this sends the result to stdout:

perl -pe 'stuff' the_file

So I normally test using -pe before switching to -pi -e. Another method is to do this:

perl -pi.bak -e 'stuff' the_file

That will make a new the_file but it will save the original as the_file.bak. This also works if there are many files (e.g., the_file1, the_file2, ...):

perl -pi.bak -e 'stuff' the_file*

All will be backed up.

Mike

_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members