我怎样才能像这样使用 tailf 和 awk 拆分输出行?
由此:
tail -f logfile.log | awk '/connected/ {print "\033[31m" $0 "\033[39m"}'
输出:
2018-01-31 10:00 user connected autentication successful
2018-01-31 10:10 user connected autentication successful
2018-01-31 10:23 user connected autentication successful
2018-01-31 10:34 user connected autentication unsuccessful
2018-01-31 10:35 user connected autentication unsuccessful
输出如下:
2018-01-31 10:00 user connected autentication successful
----------------------------------------------------------
2018-01-31 10:10 user connected autentication successful
----------------------------------------------------------
2018-01-31 10:23 user connected autentication successful
----------------------------------------------------------
2018-01-31 10:34 user connected autentication unsuccessful
----------------------------------------------------------
2018-01-31 10:35 user connected autentication unsuccessful
提前致谢!
答案1
如果您希望分隔线是白色的:
tail -f logfile.log | awk '/connected/ {print "\033[31m" $0 "\033[39m\n-----------------------------------"}'
如果您希望分隔线为红色:
tail -f logfile.log | awk '/connected/ {print "\033[31m" $0 "\n-----------------------------------\033[39m"}'
答案2
如果您想要做的只是----------------------------------------------------------
在每一行后打印,请将 ORS(输出记录分隔符)设置为您希望在每一行后出现的任何内容:
$ awk -v ORS='\033[39m\n----------------------------------------------------------\n' '/connected/ {print "\033[31m" $0}' foo
2018-01-31 10:00 user connected autentication successful
----------------------------------------------------------
2018-01-31 10:10 user connected autentication successful
----------------------------------------------------------
2018-01-31 10:23 user connected autentication successful
----------------------------------------------------------
2018-01-31 10:34 user connected autentication unsuccessful
----------------------------------------------------------
2018-01-31 10:35 user connected autentication unsuccessful
----------------------------------------------------------
在这里,我将\033[39m
从命令 移到 ORS print
,并用换行符 ( ) 将连字符括起来\n
。