#!/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