Does anyone know of a series of unix commands that allows someone to append some text to the end of a specific line in a file?
eg
Line 1
Line 2
Line 3
Line 4
I wish to append the text ", extra information" to Line 3 so that the File will now look like this:
Line 1
Line 2
Line 3, extra information
Line 4
If you want the extra information to be appended to just one line, any of the sed/awk one-liners will do.
If you want to append something to (almost) every line in the file, you should create a second file with the extra information for each line and use
paste
:in awk it's:
proper sed version:
Here is a version with portable
sed
(without -i option):Note that
myfile
is not modified, so you can recover from possible mistakes.Perl:
$. is the line number
The part before the braces is the condition: If we are in line 3, we append some text. If braces are omitted, the default action is to just print out
$0
. Redirect to a new file or pipe to another program as appropriate. You cannot redirect to the same file you are just reading. A common workaround is to redirect to a new file, and then move over if the command was successful: