Saturday, July 30, 2022

[SOLVED] sed one liner to delete replace and merge

Issue

I have file where i have control character are appearing at the end of the line and there are also blank lines in between the lines in file, i want both to be removed and the merge every two line into one.

I want to do it as a one liner sed which i'm doing into pieces.

My file.

test.log

d62150:fxn3008_d62150 - ^M
809MB 668MB 271MB

d62150:fxn4008_d62150 6227MB ^M

9465MB 9778MB 0MB

My way of doing:

$ sed -r 's/\r//g;s/-/0/g;s/([0-9]+)MB/\1/g' test.log|sed '/^$/d' |sed "N;s/\n/ /"

OUTPUT:

d62150:fxn3008_p62150 0   809 668 271
d62150:fxn4008_p62150 6227   9465 9778 0

SED Version: sed (GNU sed) 4.2.2


Solution

I believe this might be what you are after:

sed '/^$/d;:a;/\r\n*$/{N;ba};s/\r\n*//;s/MB//g;s/-/0/g' file

The idea is the following:

  1. read a line in the pattern-space
  2. /^$/d: if the line is empty, delete it and restart the cycle
  3. :a;/\r\n*$/{N;ba};: if the pattern space ends with CR followed by one or more LF, read the next line, append an LF and the line to the pattern-space. Keep on doing that until the pattern space ends with something else.
  4. s/\r\n*//: Delete all CRLF* sequences.
  5. s/MB//g;s/-/0/g : perform final substitutions.


Answered By - kvantour
Answer Checked By - Cary Denson (WPSolving Admin)