在每个unix GREP结果周围添加HTML标记

Eri*_*olf 3 html bash ubuntu grep

我正在编写一个脚本来从日志文件中获取某些文本并将其发布到html文件中.我遇到的问题是我希望grep的每个结果都在<p></p>标签内.

这是我到目前为止所拥有的:

cat my.log | egrep 'someText|otherText' | sed 's/timestamp//'
Run Code Online (Sandbox Code Playgroud)

Joh*_*024 5

使用egrepsed

你目前有:

$ echo 'timestamp otherText' | egrep 'someText|otherText' | sed 's/timestamp//'
 otherText
Run Code Online (Sandbox Code Playgroud)

要在文本周围添加para-tags,只需在sed命令中添加一个替换:

$ echo 'timestamp otherText' | egrep 'someText|otherText' | sed 's/timestamp//; s|.*|<p>&</p>|'
<p> otherText</p>
Run Code Online (Sandbox Code Playgroud)

运用 awk

$ echo 'timestamp otherText' | awk '/someText|otherText/{sub(/timestamp/, ""); print "<p>" $0 "</p>"}'
<p> otherText</p>
Run Code Online (Sandbox Code Playgroud)

或者,从文件获取输入my.log:

awk '/someText|otherText/{sub(/timestamp/, ""); print "<p>" $0 "</p>"}' my.log
Run Code Online (Sandbox Code Playgroud)