Sed wraps some lines, not all


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed wraps some lines, not all
# 1  
Old 03-16-2011
Sed wraps some lines, not all

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.
# 2  
Old 03-16-2011
try this in your 2nd step:
Code:
 sed -ir  ':a;N; $!ba; s/.\n/\t/g' infile

---------- Post updated at 01:01 ---------- Previous update was at 00:58 ----------

n and N doesn't start next cycle automatically. so a label is needed here.

---------- Post updated at 01:06 ---------- Previous update was at 01:01 ----------

or you could write
Code:
 sed -r  '/.$/{N;N;N;s/.\n/\t/g;} '

This User Gave Thanks to sk1418 For This Post:
# 3  
Old 03-16-2011
Here's my crack at it, although if you're working with huge files, it might not be a great idea to load that much into the Hold buffer:
Code:
sed -n '1h;2,$H;${g;s/\n/\t/g;s/\tReceiver/\nReceiver/g;p}' infile

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

This User Gave Thanks to LivinFree For This Post:
# 4  
Old 03-16-2011
Code:
xargs -n4 < infile |sed 's/  */\t/g'

This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 03-16-2011
Elegant solution, Rdcwayx!

Some versions of sed do not support the \t syntax.

This should work on all platforms:
Code:
xargs -n4 < file | tr " " "\t"

# 6  
Old 03-17-2011
Quote:
Originally Posted by LivinFree
Here's my crack at it, although if you're working with huge files, it might not be a great idea to load that much into the Hold buffer:
Code:
sed -n '1h;2,$H;${g;s/\n/\t/g;s/\tReceiver/\nReceiver/g;p}' infile

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

Both suggestions worked great! I don't even have to add a newline after the PowerOnHours field (no added value, really). Your solution with bash is very straightforward, perfect for my big files.

---------- Post updated at 09:43 AM ---------- Previous update was at 09:41 AM ----------

Quote:
Originally Posted by fpmurphy
Elegant solution, Rdcwayx!

Some versions of sed do not support the \t syntax.

This should work on all platforms:
Code:
xargs -n4 < file | tr " " "\t"

I respectfully disagree. This suggestion gives me the following output

Code:
Receiver    ID    =    028908221899
Serial    Number    =    WD-WCASU6774998
Current    temp    =    37C
PowerOnHours    =    17434h

Not what I am looking for.

LivinFree got it right though. Check out his solution.Smilie This is what I was looking for:

Code:
Receiver ID   = 028935846791    Serial Number = WD-WCAV90944398 Current temp  = 51C     PowerOnHours  = 8896h
Receiver ID   = 028938701381    Serial Number = WD-WCAV91967340 Current temp  = 47C     PowerOnHours  = 6856h
Receiver ID   = 028938656577    Serial Number = WD-WCAV91257990 Current temp  = 48C     PowerOnHours  = 7670h

Thanks guys, you're all aces!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

2. Shell Programming and Scripting

Use sed to print first n lines and last n lines of an output.

Use sed to print first n lines and last n lines of an output. For example: n=10 Can it be done? Thanks. (7 Replies)
Discussion started by: carloszhang
7 Replies

3. Shell Programming and Scripting

sed print all lines between second and third identical lines

I am trying to extract a table of data (mysql query output) from a log file. I need to print everything below the header and not past the end of the table. I have spent many hours searching with little progress. I am matching the regexp +-\{99\} with no problem. I just can't figure out how to print... (5 Replies)
Discussion started by: godfreydanials
5 Replies

4. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

5. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

6. Shell Programming and Scripting

sed show lines text between 2 blank lines

I have a file like blah blah blah blah this is the text I need, which might be between 1-4 lines, but always has a blank line above and below it, and is at the end of the text file the code tags don't show the trailing blank line. I started by deleting the last blank line with: ... (2 Replies)
Discussion started by: unclecameron
2 Replies

7. Shell Programming and Scripting

bash script too many fields wraps to multiple lines

Hello. I'm trying to write a script to take a 5 field file, do some math, and extend it to 9 fields. Problem is, the script keeps wrapping it to two lines, even tho 9 fields, tab separated (even comma separated) doesn't fill the screen. Even if it did, I'm eventually copying it to an excel ... (2 Replies)
Discussion started by: JoeNess
2 Replies

8. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

9. UNIX for Dummies Questions & Answers

how to use sed to get last 6 lines

dear experts, Sorry im a newbie. i have a file called INA.txt and it contains How do i get the 6 lines before "INA 2007-09-05" to appear? I tried using grep -A6, it doesnt work. Pls help. (5 Replies)
Discussion started by: aismann
5 Replies

10. UNIX for Advanced & Expert Users

Pinting a file on Solaris 8 that wraps around.

Can someone please explain how I can print a file and get the text to wrap around instead of truncating the line and missing the rest of the text? There are a few reports that were generated and I am trying to print them but after wasting 200 sheets of paper I realized that the lines were... (1 Reply)
Discussion started by: rtoba
1 Replies
Login or Register to Ask a Question