A newbie perl question
Im very new to perl, and I can't seem to figure this out:
I have a text file named data.txt that looks like this: Code:
--BEGIN DATA-- Code:
HTTPD - on |
Try this:
Code:
#!/usr/bin/perl Forrest |
Quote:
|
Although it doesn't contain any file locking stuff, you can do the whole lot as a one liner:
Code:
perl -i -n -e 'if (!/^--/) { s/,1/ - on/; s/,0/ - off/; print; }' data.txt The -i means the output of the program is saved into the input file. The -n means to execute the program for each line of the input file(s). The -e means that the following string (enclosed in single quotes) is the program. Here it is with some formatting to help you read it: Code:
if (!/^--/) { Inside the if block are two replacements, which substitute ",1" for " - on", and ",0" for " - off". The line is then printed. Note that none of these operations say what they are working on. This is a feature of Perl that many operations have a default argument, which is usually the $_ variable. This means "the current input line" as set by the <FILEHANDLE> operator, or in the case of your using the -n option, each line of the input file(s). |
Quote:
|
If you haven't got it already, I highly recommend bookmarking this: http://perldoc.perl.org/
See the top left of that page for tutorials |
All times are GMT -5. The time now is 06:57 AM. |