我正在编写一个脚本,从日志文件中获取特定文本并将其发布到html文件中。问题是我希望grep的每个结果都在<p></p>标记中。
以下是我目前掌握的情况:

cat my.log | egrep 'someText|otherText' | sed 's/timestamp//'

最佳答案

使用egrepsed
您目前拥有:

$ echo 'timestamp otherText' | egrep 'someText|otherText' | sed 's/timestamp//'
 otherText

要在文本周围放置para标记,只需在sed命令中添加一个替换:
$ echo 'timestamp otherText' | egrep 'someText|otherText' | sed 's/timestamp//; s|.*|<p>&</p>|'
<p> otherText</p>

使用awk
$ echo 'timestamp otherText' | awk '/someText|otherText/{sub(/timestamp/, ""); print "<p>" $0 "</p>"}'
<p> otherText</p>

或者,从文件获取输入:
awk '/someText|otherText/{sub(/timestamp/, ""); print "<p>" $0 "</p>"}' my.log

09-03 20:44