sed, join lines that do not match pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed, join lines that do not match pattern
# 1  
Old 04-30-2012
sed, join lines that do not match pattern

Hello,
Could someone help me with sed. I have searched for solution 5 days allready Smilie, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text:
Code:
123, foo1, bar1, short text1, dat1e, stable_pattern
124, foo2, bar2, long text
with few
lines, date, stable_pattern
125, foo3, bar3, short text2, date, stable_pattern

I need 1 string per record. So, 124 record should be ammended and all its lines should be joined in one. Output should be:
Code:
123,foo, bar,short text,date,stable_pattern
124,foo2, bar2, long text with few lines,date, stable_pattern
125,foo3,bar3,short text, date, stable_pattern

Actually i need to join lines that do not match pattern. In this sample pattern is "stable_pattern". I "construct" Smilie the following command, but it does not work:
Code:
cat text|sed -e '/stable_patter/!s/$//'

Seams the way is correct, but it does not remove the |new line| symbol:
Code:
 cat text|sed -e '/stable_patter/!s/$/_______sould_be_removed_____/'
123, foo1, bar1, short text1, dat1e, stable_pattern
124, foo2, bar2, long text_______sould_be_removed_____
with few_______sould_be_removed_____
lines, date, stable_pattern
125, foo3, bar3, short text2, date, stable_pattern


Somewhere on the internet i found another example, but it does not work for me too:
Code:
cat text|sed -e ":a;N;/stable_pattern/!s/\n//g;ta"
123, foo1, bar1, short text1, dat1e, stable_pattern
124, foo2, bar2, long text
with few
lines, date, stable_pattern
125, foo3, bar3, short text2, date, stable_pattern

Please help me to figure out this

thanks.

Last edited by petrasl; 04-30-2012 at 08:38 AM..
# 2  
Old 04-30-2012
I would use awk (nawk on Solaris):
Code:
awk '/stable_pattern$/ {print;next;} {printf("%s",$0);}' text

# 3  
Old 04-30-2012
You are my hero !!! Smilie Smilie
# 4  
Old 05-02-2012
sed, join lines that do not match pattern

Hi jlliagre,

great can you please explain this awk completly how it works here in joining lines.
Code:
awk '/stable_pattern$/ {print;next;} {printf("%s",$0);}' text

I am confuse for
Code:
next

stmt and other
Code:
printf

how it is joinging if search fails for a line.

Thanks
Krsnadasa
# 5  
Old 05-02-2012
Here are some explanations:next is skipping the remaining rule(s).

print is equivalent to printf("%s\n",$0)

printf format string doesn't specify a new line (no tailing \n), so the next line(s) are joined.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Join lines from two files based on match

I have two files. File1 >gi|11320906|gb|AF197889.1|_Buchnera_aphidicola ATGAAATTTAAGATAAAAAATAGTATTTT >gi|11320898|gb|AF197885.1|_Buchnera_aphidicola ATGAAATTTAATATAAACAATAAAA >gi|11320894|gb|AF197883.1|_Buchnera_aphidicola ATGAAATTTAATATAAACAATAAAATTTTT File2 AF197885 Uroleucon aeneum... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

4. Shell Programming and Scripting

Join all the lines matching similar pattern

I am trying to Join all the lines matching similar pattern. Example ; I wanted to join all the lines which has sam to a single line. In next line, i wanted to have all the lines with jones to a single line....etc > cat sample.txt sam 2012/11/23 sam 2012/12/5 sam 2012/12/5 jones... (2 Replies)
Discussion started by: evrurs
2 Replies

5. UNIX for Dummies Questions & Answers

Join lines on finding a pattern

I have a file with the following contents. DTP START START START DTP START DTP START DTP START I like to join the lines like this DTP START START START DTP START DTP START (2 Replies)
Discussion started by: nsuresh316
2 Replies

6. UNIX for Dummies Questions & Answers

Join the lines until next pattern match

Hi, I have a data file where data is splitted into multiple lines. And, each valid record starts with a patten date | <?xml and ends with pattern </dmm> e.g. 20120924|<?xml record 1 line1....record 1 line1....record 1 line1.... record 1 line2....record 1 line2....record 1 line2.... record 1... (3 Replies)
Discussion started by: Dipalik
3 Replies

7. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

8. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

9. Shell Programming and Scripting

sed print all lines after pattern match

HiCan someone show me how to print all lines from a file after a line matching a pattern using sed?Thanks (13 Replies)
Discussion started by: steadyonabix
13 Replies

10. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies
Login or Register to Ask a Question