I need a SED, or AWK, command to find the last line in a file, and add </pre> to it.

Such as:


Changes to:


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> and </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 cat:

( 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 sed or awk as requested, but for this sort of task, going to those tools is quite overkill.

  • This may work better for one-lining and simplicity, and the fact that I have a temp file would work better for this. I am going to try it, and then see results. – Lee Ikard Jun 22 '16 at 19:01
  • This worked better, for simplicity. – Lee Ikard Jun 22 '16 at 19:11

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 newfile.

  • I just thought of something like that, thanks. Didn't know it was that simple. – Lee Ikard Jun 22 '16 at 3:29
  • Is there a way to do this to the beginning of the file? Since this seems to be the simpler (more readable) way than using sed. – Lee Ikard Jun 22 '16 at 10:30
  • Answer updated. – Paul Jun 22 '16 at 12:12
  • Useless use of echo? :-) cat <<<'<pre>' - /path/to/the/file Of course, that becomes rather more complicated if you want more than one string either at the beginning or the end. – a CVn Jun 22 '16 at 13:27
  • The echo isn't "useless", it is outputting the string. There may be alternative ways of doing this, but the absence of the command "echo" in this method would result in an error. I like your approach though. Aside form the useless use of printf where echo would suffice :) upvoted. – Paul Jun 22 '16 at 13:54

With (GNU) 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 file.in.


$ sed -i -e '$s@\(.*\)@\1</pre>@' file.in

This will add </pre> to the last line in file.in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.