The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Merge lines into one anypager Shell Programming and Scripting 7 05-22-2008 12:20 PM
retrieved multiple lines on multiple places in a file dala Shell Programming and Scripting 8 03-14-2008 12:28 PM
merge 2 files (without repeating any lines) bluemoon1 Shell Programming and Scripting 9 10-25-2007 07:31 PM
merge multiple lines from flat file hnhegde Shell Programming and Scripting 4 12-05-2006 04:13 PM
Merge wrapped lines braindrain Shell Programming and Scripting 1 07-25-2005 04:03 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-07-2006
Registered User
 

Join Date: Aug 2006
Posts: 7
Use sed to merge multiple lines

Hi all:

I have a file in which the contents are as following:

...
This is a test
ONE
TWO
Hello, world!
XXX YYY CCC
test again
three, four
five
six
seven
world
AAA BBB QQQ
test
eight, nine
world
FFF EEE KKK
...

I want to use sed to merge all lines between a line that contains a word 'test' and a line that contains a word 'world' into one line, and ignore other lines. In other words, I want a result as following:

This is a test ONE TWO Hello, world!
test again three, four five six seven world
test eight, nine world


Any help is appreciated. Thanks in advance for your help!

Sincerely,
Susan
Reply With Quote
Forum Sponsor
  #2  
Old 08-07-2006
System Shock's Avatar
Registered User
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 395
Well, gets kind of tricky because of the arbitrary number of lines between "test" and "world", plus the arbitrary number of ines between "world" and "test"...
.,.. someone smarter than me will probably give you a better answer, but this works
Code:
 sed 's/$/ @/' file.txt | sed -n '/test/,/world/p' | sed '/world/G' | sed -e :a -e '/@$/N; s/\@\n//; ta'

Last edited by System Shock; 08-07-2006 at 08:57 PM.
Reply With Quote
  #3  
Old 08-08-2006
Registered User
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 88
Quote:
Originally Posted by xb88
In other words, I want a result as following:
This is a test ONE TWO Hello, world!
test again three, four five six seven world
test eight, nine world
Awk can do it.

Code:
awk '/test/,/world/{ if($0~/world/) ORS="\n"; else ORS=" "; print}' filename
Sample output:
This is a test ONE TWO Hello, world!
test again three, four five six seven world
test eight, nine world
Reply With Quote
  #4  
Old 08-08-2006
Registered User
 

Join Date: Aug 2006
Posts: 7
Thanks to both System Shock and vish indian! It seems there is no easy way for sed to do it. I have to use awk, perl, or something else ...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0