LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 06-06-2012, 04:27 PM   #1
buee
Member
 
Registered: May 2009
Posts: 81

Rep: Reputation: 16
Question 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?
 
Old 06-06-2012, 06:01 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,412
Blog Entries: 1

Rep: Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951Reputation: 951
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.
 
Old 06-07-2012, 08:09 AM   #3
buee
Member
 
Registered: May 2009
Posts: 81

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by sycamorex View Post
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.
 
Old 06-07-2012, 08:47 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784Reputation: 1784
How about:
Code:
awk '{ORS=($1~/^[0-9]/)?" ":"\n"}$1=$1' file
 
Old 06-07-2012, 09:38 AM   #5
buee
Member
 
Registered: May 2009
Posts: 81

Original Poster
Rep: Reputation: 16
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.
 
  


Reply

Tags
newline, return, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Script, command line, same commands, different returns (need help). SilversleevesX Linux - Newbie 4 02-04-2010 03:46 PM
Combine the Carriage Returns into one line yuanjunliang Programming 2 06-15-2009 09:04 AM
Remove New Line or Carriage return from Text File DIMonS Programming 13 10-01-2007 10:31 AM
How to remove carriage returns in a bunch of perl scripts? rebel Red Hat 4 04-12-2005 02:27 PM
Kword: How to remove carriage returns? RockyRed Linux - Newbie 1 07-21-2003 06:52 AM


All times are GMT -5. The time now is 07:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration