I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket)
Now, I know how to do this by grep'ing tailing, appending etc using temporary files to put stuff in, but I wanted to do it in one simple command rather than 5 or 6 commands with other temp files involved that I have to remove afterwards (it looks very messy)
u142115@linux2alm:~/aps/aps4/product/den> cat sc.txt
stuff
stuff
stuff
}
u142115@linux2alm:~/aps/aps4/product/den> sed '$ i this is the line' sc.txt
stuff
stuff
stuff
this is the line
}
In exaple 18-2 a file is opend with vi and some lines are added. You could check for the last } in your file and then use the O to insert lines above this one.
If you always know that your } is on the last line (without trailing newline) you can also use G (jumping to the end of the file) and the use O to insert above this line.
The content as well the vi commands are embedded in the script as a so called here document as described in the example.
You can insert or append new line and not a string to existing line with i or a command.
To append a string,
If you have the original line, you can use change command to change original line with the modified line.