Quantcast
Channel: User ozzy - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 31

Answer by ozzy for Replace lines in multiple files matching a pattern with lines from another file in order

$
0
0

With GNU awk (i.e. gawk) you could do:

gawk -i inplace '/mock/ { getline < "mock.txt" } 1' test{1..3}.txt

Like sed, gawk offers inplace editing. The above command looks for the regex-pattern mock, and when it finds it, it replaces it with the next line from mock.txt. The 1 is a pattern that always matches, and thus causes gawk to perform its default action, which is to print the (newly) read line. Note that you won't actually see this line during inplace-editing; the print-action is necessary for the output to be recorded.


Viewing all articles
Browse latest Browse all 31

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>