The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 03:20 PM
retrieved multiple lines on multiple places in a file dala Shell Programming and Scripting 8 03-14-2008 03:28 PM
merge 2 files (without repeating any lines) bluemoon1 Shell Programming and Scripting 9 10-25-2007 10:31 PM
merge multiple lines from flat file hnhegde Shell Programming and Scripting 4 12-05-2006 07:13 PM
Merge wrapped lines braindrain Shell Programming and Scripting 1 07-25-2005 07:03 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-07-2006
xb88 xb88 is offline
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
  #2 (permalink)  
Old 08-07-2006
System Shock's Avatar
System Shock System Shock is offline Forum Advisor  
Registered User
  
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 521
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 11:57 PM..
  #3 (permalink)  
Old 08-08-2006
vish_indian vish_indian is offline
Registered User
  
 

Join Date: Jun 2006
Location: Delhi, India
Posts: 92
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
  #4 (permalink)  
Old 08-08-2006
xb88 xb88 is offline
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 ...
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0