Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed wraps some lines, not all

Status
Not open for further replies.

kristo5747

Programmer
Mar 16, 2011
41
US
Greetings. I am using SED to cleanup files that are laid out like so:

Code:
ReceiverID=028936004663
SerialNumber=WD-WCAV95708405
Currenttemp=44C
PowerOnHours=3663h
ReceiverID=028923894902
SerialNumber=WD-WCAV9A766701
Currenttemp=49C
PowerOnHours=2215h

My boss wants files like this one to be tab ("\t") delimited like so

Code:
ReceiverID=...(tab)SerialNumber=...(tab)Currenttemp=...(tab)PowerOnHours=...(newline)
ReceiverID=...(tab)SerialNumber=...(tab)Currenttemp=...(tab)PowerOnHours=...(newline)...

1) first, I added a newline to mark each record
Code:
sed -i 's/h/h\n/g' infile
,
2) then, I added the the tab delimiter
Code:
    sed -i '/.$/N; s/.\n/\t/' infile

It works but strangely, not everywhere. This is the output I get

Code:
ReceiverID=...(tab)SerialNumber=...(tab???)
Currenttemp=...(tab)PowerOnHours=...(newline)
ReceiverID=...(tab)SerialNumber=...(tab???)
Currenttemp=...(tab)PowerOnHours=...(newline)

What am I missing?? I welcome your input. Thanks.
 
Hi

With your theory you have to repeat step 2 again.

But I would not do it. If other lines will contain "h", your code will fail. Anyway, your code is removing each group's first 3 lines' last character.

Personally I prefer to identify the key of the groups' last line :
Code:
sed -n ':l;H;n;/^PowerOnHours/!bl;H;s/.*//;x;s/\n//;s/\n/\t/g;p' /input/file

[gray]# or[/gray]

sed -n ':l;/\nPowerOnHours/!{N;bl};s/\n/\t/g;p' /input/file
But if you are sure there will be always groups of 4 lines, can be done easier :
Code:
sed -n 'N;N;N;s/\n/\t/g;p' /input/file


Feherke.
 
Another way, with awk:
Code:
awk '{x[NR%4]=$0}!(NR%4){for(i=1;i<4;++i)printf "%s\t",x[i];print}' infile

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
After *lots* of trials/errors, I got this (with help from people smarter than me):

Code:
#! /bin/bash
while read line; do
    [[ "$line" =~ PowerOnHours ]] && { 
        printf '%s\n' "$line"
    } || {
        printf '%s\t' "$line"
    }
done <myfile

I don't even need to add the newline after PowerOnHours (no added value, really). It gets me the output I was looking for

Code:
Receiver ID   = 028918576472    Serial Number = WD-WCAUK0635287 Current temp  = 50C     PowerOnHours  = 12972h
Receiver ID   = 028968505835    Serial Number = WD-WCAUH1726359 Current temp  = 48C     PowerOnHours  = 9591h

Thanks for your time.[smile2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top