I\'m trying to add a line of text to the middle of a text file in a bash script. Specifically I\'m trying add a nameserver to my /etc/resolv.conf file. As it stands, resol
Here's a Perl solution:
perl -lne 'if (not $f and /^nameserver/){ print "nameserver 127.0.0.1"; $f=1 }; print' resolv.conf
-n loop around every line of the input file, do not automatically print every line
-l removes newlines before processing, and adds them back in afterwards
-e execute the perl code
$f is used as a flag to indicate that the nameserver string has already been found