![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
Quote:
Code:
awk '/test/,/world/{ if($0~/world/) ORS="\n"; else ORS=" "; print}' filename
This is a test ONE TWO Hello, world! test again three, four five six seven world test eight, nine world |
|
#4
|
|||
|
|||
|
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 ...
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|