I need a SED, or AWK, command to find the last line in a file, and add
</pre> to it.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
You can also use what is known as a compound command. That's a list of commands, which the shell treats as a single command for the purpose of anything external to the compound command.
Yes, that's somewhat of a recursive definition; an example makes it easier to understand.
For example, to surround a file with
</pre>, you might use a command like:
( printf '<pre>' ; cat originalfile ; printf '</pre>' ) > newfile
If you don't have a temporary file, but rather want to surround the output of some command with something else, you can inject that command instead of the
( printf '<pre>' ; find / -type d -print ; printf '</pre>' ) > somefile
This all works by applying the redirection to the output of the whole compound command, instead of just its parts.
Of course, this doesn't use
awk as requested, but for this sort of task, going to those tools is quite overkill.
How about this:
echo "</pre>" >> /path/to/the/file
Or at the beginning is a bit trickier, you'd need to use a different file for the output.
echo "<pre>" | cat - /path/to/the/file > /path/to/the/newfile
cat means concatenate, and it has two arguments in this case: the
- means stdin, so the first part of the concatenated file is whatever is piped to it (in this case the
echo). The second argument is the file you want to prepend to. The concatenated files are then output to
sed (as per the tag in the question):
$ sed -i -e '$a\</pre>' file.in
This will add a line with
</pre> on it to
$ sed -i -e '$s@\(.*\)@\1</pre>@' file.in
This will add
</pre> to the last line in