Perl search & replace
How can I use search and replace to change the contents of a file.
I need to be able to change the 'all to none' and/or 'none to all' without having to manually do it every time for hundreds of lines.
below is a sample text file with lines numbers on the right. I know how to open and close the file for writing already.
Lines
1 define_filter absolute_minimal_filter ------- all
2 define_filter atma_basic_filter -------- ----- none
3 define_filter atma_communication_filter -- all
4 define_filter atma_health_filter ------------- none
5 define_filter atma_misc_filter --------------- all
6 define_filter atma_performance_filter ---- none
7 define_filter atma_supplemental_filter ---- none
With a well-chosen string in place of "temp" (in two places):
sed 's/all/temp/g;s/none/all/g; s/temp/none/g' input_file > output_file
or without a temp string if "all" and "none" never appear on the same line and always end the line:
sed 's/all$/none/;t;s/none$/all/' input_file > output_file
Just out of interest, the Perl solution is pretty similar to the sed one:
perl -pi.bak -e 's/all$/none/ || s/none$/all/' input_file
EDIT: I missed the "||" first time. As separate statements, the second would undo the first. D'oh!