Reputation: 130
I have been having a bit of a trouble trying to write a command that can merge 2 lines in a text file and then enter a new line where there is a delimiter.I have researched this thoroughly and the only thing i could find was to merge every second line then add a delimiter. This is not what i need, as i already have a delimiter in the file that separates every 2 lines i need to merge. The file currently contains the following (to get these results i am using the command at the top):
grep -C 2 "some text" somefile.log | grep -A1 "somepattern" | cut -d ' ' -f -1,2,3,4 | cut -c 1-19,118,150-163,130-132 ^C
This is the file:
2015-12-29 08:55:43 sg-somepattern
2015-12-29 08:55:43 N22
--
2015-12-29 08:56:59 sg-somepattern
2015-12-29 08:56:59 N22
--
2015-12-29 08:58:07 sg-somepattern
2015-12-29 08:58:07 N22
--
2015-12-29 09:19:32 sg-somepattern
2015-12-29 09:19:32 N22
--
2015-12-29 22:05:57 sg-somepattern
2015-12-29 22:05:57 N22
Now the results i need should look like this:
2015-12-29 08:58:07 sg-somepattern 2015-12-29 08:58:07 N22
2015-12-29 09:19:32 sg-somepattern 2015-12-29 09:19:32 N22
2015-12-29 22:05:57 sg-somepattern 2015-12-29 22:05:57 N22
Having used sed 'N;s/\n/ /'
has not worked and just joined every second line mixing in the lines with the delimiters also.
Is there any way to achieve this with a built in command? I am not familiar with awk so i would struggle to write this on my own. Any help would be appreciated.
Upvotes: 1
Views: 553
Reputation: 58430
This might work for you (GNU sed):
sed '/^--/d;N;s/\n/ /' file
Delete the delimiters and join every pair of lines left.
Upvotes: 0
Reputation: 289775
I would use awk
for this:
$ awk -v RS="--" -F"\n" 'NF>=3{print $(NF-2), $(NF-1)}' file
2015-12-29 08:55:43 sg-somepattern 2015-12-29 08:55:43 N22
2015-12-29 08:56:59 sg-somepattern 2015-12-29 08:56:59 N22
2015-12-29 08:58:07 sg-somepattern 2015-12-29 08:58:07 N22
2015-12-29 09:19:32 sg-somepattern 2015-12-29 09:19:32 N22
2015-12-29 22:05:57 sg-somepattern 2015-12-29 22:05:57 N22
This sets the delimiter --
to the record separator. This way, every record is a block. Then, it is a matter of printing the specific lines that we want.
Upvotes: 1