Perl one-liners that gulp entire files for pattern matching
Summary: instead of the -pi switch, enter -p0777i, and remember to use the ms modifier in the regular expression.
Today I wanted to keep everything in each file, after a certain pattern, the last time that pattern was seen. One way of doing that is two write a perl one-liner that treats the file as one big long string, search and replace for the pattern and save away what is left.
The pattern for doing this is something like this:
find directory_name -name "*.txt" -print0|xargs -0 perl -p0777i -e 's!search_pat!replace_pat!gms;
...if you want to find all txt files under directory_name and replace search_pattern with replace_pattern, and do that:
- many times if necessary (g)
- over multiple lines (m),
-
expanding dot "." to also match newlines (s).
Now, the -0 flag in perl specifies (in octal) the input record separator (new lines in a text file). If we rely on the fact that no 0x777 character exists, we can swallow each file in one big gulp, and then do the search and replace over many lines. So:
Read more: Link - Search and replace across many files with a perl one-liner