开发者

Sed wraps some lines, not all

Still dealing with quirky files (see my pre开发者_如何学编程vious post), I am using SED to cleanup some that are laid out like so:

....Receiver ID   = 028912781755
    Serial Number = WD-WCAUH0546786
    Current temp  = 50C
    PowerOnHours  = 13166h
    Receiver ID   = 028920310381
    Serial Number = WD-WCAUH0898333
    Current temp  = 51C
    PowerOnHours  = 9099h...

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

Receiver ID = 028912781755 Serial Number = WD-WCAUH0546786 Current temp  = 50C PowerOnHours  = 13166h

This is my sed code:

sed -e '/.$/N; s/.\n/\t/'

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

Receiver ID   = 028920310381 Serial Number = WD-WCAUH0898333 
Current temp  = 51 PowerOnHours  = 9099h

====================================================================== I need to be more specific. All suggestions I got produce the same result: it appends everything on one singly line. Not what I am looking for.

I am looking for:

Receiver ID = ...<tab>Serial Number = ...<tab>Current temp  = ...<tab>PowerOnHours  = ...<tab><carriage return>
Receiver ID = ...<tab>Serial Number = ...<tab>Current temp  = ...<tab>PowerOnHours  = ...<tab> 


Number of fields vary but end in PowerOnHours

awk 'ORS=/PowerOnHours/?RS:"\t"' ./infile

Proof of Concept

$ awk 'ORS=/PowerOnHours/?RS:"\t"' receiverid
Receiver ID   = 028912781755    Special Field = foo bar baz     Serial Number = WD-WCAUH0546786 Current temp  = 50C     PowerOnHours  = 13166h
Receiver ID   = 028920310381    Serial Number = WD-WCAUH0898333 Current temp  = 51C     PowerOnHours  = 9099h
Receiver ID   = 028912781755    Serial Number = WD-WCAUH0546786 Current temp  = 50C     PowerOnHours  = 13166h
Receiver ID   = 028920310381    Serial Number = WD-WCAUH0898333 Current temp  = 51C     PowerOnHours  = 9099h

*Note the Special Field on the first line


Number of fields between records are the same

awk 'ORS=NR%4?"\t":RS' ./infile

Proof of Concept

$ awk 'ORS=NR%4?"\t":RS' ./infile
Receiver ID   = 028912781755    Serial Number = WD-WCAUH0546786 Current temp  = 50C     PowerOnHours  = 13166h
Receiver ID   = 028920310381    Serial Number = WD-WCAUH0898333 Current temp  = 51C     PowerOnHours  = 9099h
Receiver ID   = 028912781755    Serial Number = WD-WCAUH0546786 Current temp  = 50C     PowerOnHours  = 13166h
Receiver ID   = 028920310381    Serial Number = WD-WCAUH0898333 Current temp  = 51C     PowerOnHours  = 9099h


Give this a try:

sed '/^Receiver/N;N;N;s/\n/\t/g' inputfile

Explanation:

  • /^Receiver/N;N;N; - Each time a line that begins with "Receiver" is read, append the next three lines.
  • s/\n/\t/g - Replace the embedded newlines with tabs

Sample output:

Receiver ID   = 028912781755        Serial Number = WD-WCAUH0546786        Current temp  = 50C        PowerOnHours  = 13166h
Receiver ID   = 028920310381        Serial Number = WD-WCAUH0898333        Current temp  = 51C        PowerOnHours  = 9099h

(I exaggerated the tabs for effect.)


you can use awk

$ cat  file
Receiver ID   = 028912781755
Serial Number = WD-WCAUH0546786
Current temp  = 50C
PowerOnHours  = 13166h
Receiver ID   = 028920310381
Serial Number = WD-WCAUH0898333
Current temp  = 51C
PowerOnHours  = 9099h...

$ awk 'BEGIN{RS="Receiver";OFS="\t"}NF>1{$1=$1;print "Receiver\t"$0}' file
Receiver        ID      =       028912781755    Serial  Number  =       WD-WCAUH0546786 Current temp    =       50C     PowerOnHours    =       13166h
Receiver        ID      =       028920310381    Serial  Number  =       WD-WCAUH0898333 Current temp    =       51C     PowerOnHours    =       9099h...


sed ':a          
N;/\nReceiver/{
P;D
}          
s/\n/X/;ta'


As written, that will join the second line to the first, then go to the third line and join the fourth to it, etc.

sed ':b; /^$/n; N; s/.\n\(.\)/\t\1/; tb'

should loop appending non-empty lines. (Corrected to actually catch blank lines in runs.)


cat file | tr '\n' '\t' will work, too

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜