Join the lines until next pattern match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Join the lines until next pattern match
# 1  
Old 10-01-2012
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.
Code:
20120924|<?xml record 1 line1....record 1 line1....record 1 line1....
record 1 line2....record 1 line2....record 1 line2....
record 1 line3....record 1 line3....</dmm>
20120924|<?xml record 2 line1....record 2 line1....record 2 line1....
record 2 line2....record 2 line2....</dmm>
20120924|<?xml record 3 line1....record 3 line1
record 3 line2
record 3 line3....record 3 line3
record 3 line4....record 3 line4....record 3 line4....</dmm>

And, to process this data further i need one record as a single line:

output required:
Code:
20120924|<?xml record 1 line1....record 1 line1....record 1 line1....record 1 line2....record 1 line2....record 1 line2....record 1 line3....record 1 line3....</dmm>
20120924|<?xml record 2 line1....record 2 line1....record 2 line1....record 2 line2....record 2 line2....</dmm>
20120924|<?xml record 3 line1....record 3 line1
record 3 line2record 3 line3....record 3 line3record 3 line4....record 3 line4....record 3 line4....</dmm>


Can someone please help me out to get the desired output.

Thanks
Dipali
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-01-2012 at 07:03 AM.. Reason: code tags,please!
# 2  
Old 10-01-2012
This should work for you:
Code:
awk '/<\?xml/ && p{print p;p=""}{p=p $0}END{if(p) print p}' file

These 2 Users Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-01-2012
try this..
Code:
awk '{if($0 ~ /^201/){if(s){print s;s=$0}else{s=$0}}else{s=s" "$0}}END{print s}' file

# 4  
Old 10-01-2012
With sed: sed -n 'H;/<\/dmm>$/{x;s/\n//g;p;n;h}' <file

EDIT: sorry, the above command doesn't work well if there are "one-line" records.
We can fix it this way: sed -n 'H;:a /<\/dmm>$/{x;s/\n//g;p;n;h;b a}' <file
--
Bye

Last edited by Lem; 10-01-2012 at 09:43 AM..
 
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

Printing next 6 lines from of pattern match

Hi, i have a big file having many opcodes. if (opcode="01110000000100000000" ) then --fadd result.opcode := "01110000000100000000"; result.s0 := '1'; result.s1 := '1'; result.s2 := '0'; result.inst := '0'; result.scalar := '1';... (7 Replies)
Discussion started by: twistedpair
7 Replies

4. 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

5. Shell Programming and Scripting

Print lines that do not match the pattern

I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times. Example ........ abcd...... .........abcd...... .........abcd......... (4 Replies)
Discussion started by: sunny1234
4 Replies

6. 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

7. 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

8. UNIX for Dummies Questions & Answers

sed, join lines that do not match pattern

Hello, Could someone help me with sed. I have searched for solution 5 days allready :wall:, but cant find. Unfortunately my "sed" knowledge not good enough to manage it. I have the text: 123, foo1, bar1, short text1, dat1e, stable_pattern 124, foo2, bar2, long text with few lines, date,... (4 Replies)
Discussion started by: petrasl
4 Replies

9. 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

10. UNIX for Dummies Questions & Answers

retrieve lines that match a pattern

Hi, I would like to know how can I get lines from a text file that match no more than 2 '>'. Example: Input file: a >cr1 4 a>b b>c a >cr2 5 a>b Output file: a >cr2 5 a>b Thanks in advance (2 Replies)
Discussion started by: fadista
2 Replies
Login or Register to Ask a Question