You can use this :
sed -n '/\[Z\]$/ s/^/<font>/;/\[Z\]$/ s/$/<\/font>/p' file.txt
As you will use it over STDOUT of another command, so use it as e.g. :
some command | sed -n '/\[Z\]$/ s/^/<font>/;/\[Z\]$/ s/$/<\/font>/p'
It basically has two parts :
/\[Z\]$/ s/^/<font>/
will match the line that has [Z]
at last, then will add <font>
at the start of the line by s/^/<font>/
/\[Z\]$/ s/$/<\/font>/
will match [Z]
at the end of the line and then will add </font>
at the end of the line using /$/<\/font>/
.
Example :
$ cat file.txt
service:clus1-svr clus1-node2 started [Z]
service:clus1-svr clus1-node2 started
$ sed -n '/\[Z\]$/ s/^/<font>/;/\[Z\]$/ s/$/<\/font>/p' file.txt
<font>service:clus1-svr clus1-node2 started [Z]</font>
As @glennjackman has pointed out you can also use a grouping construct to do all the replacements for a single match :
$ sed -n '/\[Z\]$/ { s/^/<font>/; s/$/<\/font>/p }' file.txt
<font>service:clus1-svr clus1-node2 started [Z]</font>
How about a bash
solution :
while IFS= read -r line; do [[ $line =~ \[Z\]$ ]] && echo "<font>${line}</font>"; done <file.txt
You can use it as :
some command | while IFS= read -r line; do [[ $line =~ \[Z\]$ ]] && echo "<font>${line}</font>"; done
This uses the same logic as the sed
one. If the line ends with [Z]
([[ $line =~ \[Z\]$ ]]
), then the line is printed in desired format.
Example :
$ while IFS= read -r line; do [[ $line =~ \[Z\]$ ]] && echo "<font>${line}</font>"; done <file.txt
<font>service:clus1-svr clus1-node2 started [Z]</font>