Question

I am analyzing log files from my server(particular few lines from there). But those lines are having around 1000 to 2000 characters in length.

Here is an example how the lines are coming out with grep and less with following command.

tail -n 1000 log.log | grep 'SOME_TEXT' | less

Output:

random text SOME_TEXT another 1000 chars
random SOME_TEXT 2000 chars
text text SOME_TEXT 2000 chars

But its hard for me to separate them as those are screen full of texts(also not pleasant for eyes either). I want to have my output like blow:

random text SOME_TEXT another 1000 chars

random SOME_TEXT 2000 chars

text text SOME_TEXT 2000 chars

And I have modified my above command with sed:

tail -n 1000 log.log | grep 'SOME_TEXT' | sed 's/\n/\n\n/' | less

It didn't work, so I tried with tr next:

tail -n 1000 log.log | grep 'SOME_TEXT' | tr '\n' '\n\n' | less

It didn't work with me either. What am I missing here?

Was it helpful?

Solution

This might work for you:

sed G file

OTHER TIPS

This will search for pattern and print only pattern with an extra blank line after it.

awk '/SOME_TEXT/ {print $0 RS}' file

Example:

cat file
test
more
SOME_TEXT my data
more
even more
SOME_TEXT need more
more data
yes

awk '/SOME_TEXT/ {print $0 RS}' file
SOME_TEXT my data

SOME_TEXT need more

To print all text, with a blank line after pattern

awk '8; /SOME_TEXT/ {print ""}' file
test
more
SOME_TEXT my data

more
even more
SOME_TEXT need more

more data
yes

You can try this sed,

sed '/SOME_TEXT/s/.*/&\n/g' logfile.log

Or

sed '/SOME_TEXT/G' logfile.log
tail -n 1000 log.log | sed -n '/SOME_TEXT/ {G;p;}' 

use the hold buffer

u can also number the line as separation

tail -n 1000 log.log | sed -n '/SOME_TEXT/ {=;p;}'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top