1

How can I split output lines using tailf and awk like this?

From this:

tail -f logfile.log | awk '/connected/ {print "\033[31m" $0 "\033[39m"}'

Output:

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

To this output:

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

Thanks in advance!

2

If you want the separator lines to be white:

tail -f logfile.log | awk '/connected/ {print "\033[31m" $0 "\033[39m\n-----------------------------------"}'

If you want the separator line to be red:

tail -f logfile.log | awk '/connected/ {print "\033[31m" $0 "\n-----------------------------------\033[39m"}'
0

If all you want to do is print ---------------------------------------------------------- after every line, set the ORS (output record separator) to whatever you want to come after every line:

$ 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
----------------------------------------------------------

Here, I moved the \033[39m to the ORS from the print command, and surrounded the hyphens with newlines (\n).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.