1
I need a SED, or AWK, command to find the last line in a file, and add </pre>
to it.
Such as:
LINELINELINE
Changes to:
LINELINELINE</pre>
1
I need a SED, or AWK, command to find the last line in a file, and add </pre>
to it.
Such as:
LINELINELINE
Changes to:
LINELINELINE</pre>
1
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.
0
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 – 2016-06-22T03:29:04.757
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 – 2016-06-22T10:30:08.647
Answer updated. – Paul – 2016-06-22T12:12:07.173
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 – 2016-06-22T13:27:21.313
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 – 2016-06-22T13:54:13.820
@Paul At least I didn't use a format string for printf
. – a CVn – 2016-06-22T19:44:38.177
0
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
.
Alternatively:
$ sed -i -e '$s@\(.*\)@\1</pre>@' file.in
This will add </pre>
to the last line in file.in
.
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 – 2016-06-22T19:01:16.630
This worked better, for simplicity. – Lee Ikard – 2016-06-22T19:11:32.670