LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Script remove line breaks and carriage returns (http://www.linuxquestions.org/questions/linux-newbie-8/bash-script-remove-line-breaks-and-carriage-returns-948883/)

buee 06-06-2012 04:27 PM

Bash Script remove line breaks and carriage returns
 
I have the need to turn this:

Code:

1  server=hotspot1 name="tsmith" password="password" profile=package5
    uptime=22m45s bytes-in=2238756 bytes-out=14770276 packets-in=14837
 2  server=hotspot1 name="jbob" password="password" profile=package5
    uptime=3m3s bytes-in=92196 bytes-out=952379 packets-in=849

into this (a few hundred times):

Code:

1  server=hotspot1 name="tsmith" password="password" profile=package5 uptime=22m45s bytes-in=2238756 bytes-out=14770276 packets-in=14837
 2  server=hotspot1 name="jbob" password="password" profile=package5 uptime=3m3s bytes-in=92196 bytes-out=952379 packets-in=849

Unfortunately this is an excerpt from a file generated by a router and I do not have any control over how the file is formatted.

I used this:

Code:

sed -e :a -e '$!N;s/\r\n    //;ta'
and that produces what I want, but for group 1 only, it doesn't repeat for group 2, group 3, etc. I also tried running that command in a while read $line loop:

Code:

    uptime=22m45s bytes-in=2238756 bytes-out=14770276 packets-in=14837
 2  server=hotspot1 name="jbob" password="password" profile=package5
    uptime=3m3s bytes-in=92196 bytes-out=952379 packets-in=849

And it appears to delete the first line of the text, then no affect after that.

Anyone know how to accomplish this?

sycamorex 06-06-2012 06:01 PM

Is that what you're after?
Code:

sed '/server/ N;s/\n    //g' file
Please note there are some spaces after \n
I don't know if they are in the original output. They do exist in what I copied from your post.

buee 06-07-2012 08:09 AM

Quote:

Originally Posted by sycamorex (Post 4697288)
Is that what you're after?
Code:

sed '/server/ N;s/\n    //g' file
Please note there are some spaces after \n
I don't know if they are in the original output. They do exist in what I copied from your post.

Not quite. That ended up deleting up to:

Code:

uptime=22m45s bytes-in=2238756 bytes-out=14770276 packets-in=14837
On each iteration.

grail 06-07-2012 08:47 AM

How about:
Code:

awk '{ORS=($1~/^[0-9]/)?" ":"\n"}$1=$1' file

buee 06-07-2012 09:38 AM

This is probably one of the worst possible ways to do it, but I finally got it figured out and formatted the way I need it:

Code:

while [ $i -le $lines ] ; do
        cat temp$i | head -2 | sed -e :a -e '$!N;s/\r\n    //;ta' >> hsoutput.txt
        sed '1,2d' temp$i > temp$(( $i + 1 ))
        rm -rf temp$i
        i=$(( $i + 1 ))
done

Then I just do an rm -rf temp* at the end of the script to clean up the extra garbage.


All times are GMT -5. The time now is 09:21 AM.