input: desired output:
file1: file1:
line1 line1
opt=foo opt=bar
line3 line3
file2: file2:
line1 line1
line2 line2
line3 line3
####
input: desired output:
file1: file1:
line1 line1
line2 line2
line3 line3
file2: file2:
line1 line1
line2 line2
line3 line3
opt=bar
##
##
$ perl -i -pe '$f||=s/opt=.*/opt=bar/; END { print "opt=bar\n" if !$f }' file1 file2
##
##
#!/bin/bash
#echo -e "line1\nopt=foo\nline3" >file1 # case 1
echo -e "line1\nline2\nline3" >file1 # case 2
echo -e "line1\nline2\nline3" >file2
echo --- cat before ---
cat file1 file2
echo --- perl output to stdout ---
perl -i -pe '$f||=s/opt=.*/opt=bar/; $_.="opt=bar\n" if eof() && !$f' file1 file2
echo --- cat after ---
cat file1 file2
##
##
--- cat before ---
line1
line2
line3
line1
line2
line3
--- perl output to stdout ---
line3
opt=bar
--- cat after ---
line1
line2
line3
line1
line2
##
##
--- cat before ---
line1
line2
line3
line1
line2
line3
--- perl output to stdout ---
--- cat after ---
line1
line2
line3
line1
line2
line3
opt=bar