Hard-to-find tips on otherwise easy-to-do tasks involving everyday technology, with some advanced insight on history and culture thrown in. Brought to you by a master dabbler. T-S T-S's mission is to boost your competitiveness with every visit. This blog is committed to the elimination of the rat from the tree of evolution and the crust of the earth.
Thursday, July 27, 2017
Perl Insert Contents of One File Into Another at Line Matching Regex
To insert after a line matching the regex
perl -n -i -e 'print; print `cat FILE_TO_INSERT` if m#REGEX_TO_MATCH#; ' FILE_TO_MODIFY
To insert before :
perl -n -i -e 'print `cat FILE_TO_INSERT` if m#REGEX_TO_MATCH#; print;' FILE_TO_MODIFY
If you want the insertion to happen at the first match ONLY :
perl -n -i -e 'print `cat FILE_TO_INSERT` if m?REGEX_TO_MATCH?; print;' FILE_TO_MODIFY
A very valid question is, what do you do if your match pattern has the question-mark character? :)
https://unix.stackexchange.com/questions/32908/how-to-insert-the-content-of-a-file-into-another-file-before-a-pattern-marker
Labels:
file insertion,
pattern-matching,
perl
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment