How to combine multilpe sed commands into one

Issue

I am unable to do the both replace and delete action into one go while using sed hence using multiple occurrence of the sed.

Raw datafile

d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB

d62150 fxn4008_d62150_bl 6227MB ^M

9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB

d62150 fxn4008_d62150_bl 6227MB ^M

9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB

d62150 fxn4008_d62150_bl 6227MB ^M

9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB

d62150 fxn4008_d62150_bl 6227MB ^M

9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB

d62150 fxn4008_d62150_bl 6227MB ^M

9465MB 9778MB 0MB
d62150 fxn3008_d62150 - ^M
809MB 668MB 271MB

d62150 fxn4008_d62150_bl 6227MB ^M

9465MB 9778MB 0MB

The current Solution i am using is :

sed -i -e 's/\r//g; s/-/0/g; s/MB//g'  log1
sed '/^[[:space:]]*$/d' "$datafile.log1"| paste -d " " - - > log2
awk '{$1=$1}1' OFS="," "$datafile.log2" > log

Test1 out this will remove the control chars and MB while replace the - to 0 s desired.

$ sed -e 's/\r//g; s/-/0/g; s/MB//g' test22.log
d62150 fxn3008_d62150 0
809 668 271

d62150 fxn4008_d62150_bl 6227

9465 9778 0
d62150 fxn3008_d62150 0
809 668 271

d62150 fxn4008_d62150_bl 6227

9465 9778 0
d62150 fxn3008_d62150 0
809 668 271

d62150 fxn4008_d62150_bl 6227

9465 9778 0
d62150 fxn3008_d62150 0
809 668 271

d62150 fxn4008_d62150_bl 6227

9465 9778 0
d62150 fxn3008_d62150 0
809 668 271

d62150 fxn4008_d62150_bl 6227

9465 9778 0
d62150 fxn3008_d62150 0
809 668 271

d62150 fxn4008_d62150_bl 6227

9465 9778 0

Test2 out However i see the another way of working like below which is mupltiple pipe sequences and paste command for joining lines and subsequently awk for making it comma separated fields.

$ sed -e 's/\r//g; s/-/0/g; s/MB//g' test22.log | sed '/^$/d' |  paste -d " " - -|awk '{$1=$1}1' OFS=","
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0
d62150,fxn3008_d62150,0,809,668,271
d62150,fxn4008_d62150_bl,6227,9465,9778,0

Please help make is easier or one liner.

Solution

You never need sed when you’re using awk. Using any awk:

awk '
    BEGIN { OFS="," }
    !NF { next }          # Discard all empty lines
    (++c) % 2 {           # Select odd-numbered of remaining lines
        prev = $0         # Just save the line in a variable.
        next
    }
    {                     # This is an even numbered non-empty line
        $0 = prev " " $0  # Attach the previous line to the front of it
        gsub(/-/,0)
        gsub(/\r|MB/,"")
        $1 = $1           # Force replacement of all FS with OFS
        print
    }
' file

Answered By – Ed Morton

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published