Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding additional newline between each line

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?

like image 413
Sabuj Hassan Avatar asked Dec 12 '22 07:12

Sabuj Hassan


2 Answers

This might work for you:

sed G file
like image 148
potong Avatar answered Jan 02 '23 09:01

potong


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
like image 22
Jotne Avatar answered Jan 02 '23 09:01

Jotne