Joining lines and find pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining lines and find pattern
# 1  
Old 07-24-2008
Joining lines and find pattern

I have a file that does not have much formatting. I would like every time it finds a tag (something that starts with < and end with > like <Tag1> ) to be at the beginning of the line and joined with the next line. I tried sed with append and can not get it right and also hold and get in sed without luck.

Ex. I have a file that look something like this:

I need to send you a file today and I
need to talk to you. <Tagger1> Ok, send
me the file. <Tag2> I will if you can give
me an address. <Ttt3> I will give you
an address as soon as I can talk
to Mark in shipping. <Tagg2> Alright talk
to Mark and get back to me. <Taggg4> Ok,
will do. <Tagger4> I talk to Mark. <Tg4> What
did he say?. <Tagger3> He gave me the business
address.


It should look like this:

I need to send you a file today and I
need to talk to you.
<Tagger1> Ok, send me the file.
<Tag2> I will if you can give me an address.
<Ttt3> I will give you an address as soon as I can talk
to Mark in shipping.
<Tagg2> Alright talk to Mark and get back to me.
<Taggg4> Ok, will do.
<Tagger4> I talk to Mark.
<Tg4> What did he say?.
<Tagger3> He gave me the business address.



Any suggestion on how to search and join lines and separate tags onto it's line would be appreciated.
# 2  
Old 07-24-2008
Tools One oddity still there

Code:
> cat infile | sed "s/</~</g" | tr "\n" " "  | tr "~" "\n" ; echo 
I need to send you a file today and I need to talk to you. 
<Tagger1> Ok, send me the file. 
<Tag2> I will if you can give me an address. 
<Ttt3> I will give you  an address as soon as I can talk to Mark in shipping. 
<Tagg2> Alright talk to Mark and get back to me. 
<Taggg4> Ok, will do. 
<Tagger4> I talk to Mark. 
<Tg4> What did he say?. 
<Tagger3> He gave me the business  address.

Not sure what your logic was for keeping certain lines together, while combining others. Also, my final "echo" is to force out a new-line.
# 3  
Old 07-24-2008
Code:
cat unf.txt |tr "\n" " " | sed s/\</\\n\</g


Last edited by Ikon; 07-24-2008 at 02:26 PM..
# 4  
Old 07-24-2008
That did it

Quote:
Originally Posted by joeyg
Code:
> cat infile | sed "s/</~</g" | tr "\n" " "  | tr "~" "\n" ; echo 
I need to send you a file today and I need to talk to you. 
<Tagger1> Ok, send me the file. 
<Tag2> I will if you can give me an address. 
<Ttt3> I will give you  an address as soon as I can talk to Mark in shipping. 
<Tagg2> Alright talk to Mark and get back to me. 
<Taggg4> Ok, will do. 
<Tagger4> I talk to Mark. 
<Tg4> What did he say?. 
<Tagger3> He gave me the business  address.

Not sure what your logic was for keeping certain lines together, while combining others. Also, my final "echo" is to force out a new-line.

Thanks that did it. I have spent so much time trying to get it right and your statement solved the problem. You are a genius.
# 5  
Old 07-25-2008
Code:
 perl -ne '$_=~ s/(<.*>)/\n$1/g;print $_;' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find pattern; grep n lines before and after

Hi, I need help to grep a specific part of a log file (bold). 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: } 24/2/2017-16:57:17.056 frosti-1 M3UA-Tx: extensionContainer <Not Present> 24/2/2017-16:57:17.056... (8 Replies)
Discussion started by: vasil
8 Replies

2. Shell Programming and Scripting

Using awk or sed to find a pattern that has lines before and after it

Dear gurus, Please help this beginner to write and understand the required script. I am looking for useing awk for sed. I have a few thousand lines file whose contain are mostly as below and I am trying to achieve followings. 1. Find a string, say user1. Then hash the line containing the... (6 Replies)
Discussion started by: ran_bon_78
6 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. Shell Programming and Scripting

[awk] find pattern, change next two lines

Hi, hope you can help me... It seems like a straightforward problem, but I haven't had any success so far using my basic scripting and awk "skills": I need to find a pattern /VEL/ in an input file that looks like this: 1110SOL OW25489 1.907 7.816 26.338 -0.4365 0.4100 -0.0736 ... (3 Replies)
Discussion started by: origamisven
3 Replies

5. Shell Programming and Scripting

Gawk Find Pattern Print Lines Before and After

Using grep I can easily use: cvs log |grep -iB 10 -A 10 'date: 2013-10-30' to display search results and 10 lines before and after. How can this be accompished using gawk? (4 Replies)
Discussion started by: metallica1973
4 Replies

6. UNIX for Dummies Questions & Answers

FIND matching pattern of lines in a file

I need to search for two patterns in a file and find number of matching lines. find . -type f | xargs grep "DROP TABLE" | wc -l find . -type f | xargs grep "DROP SYNONYM" | wc -l The above code works. However I am looking at finding a commnd that will simplify as on a singe command... (2 Replies)
Discussion started by: Siva SQL
2 Replies

7. Shell Programming and Scripting

how to find pattern and discard lines before it?

Hi all, I'd like to search a file for the first occurence of the phrase "PLASTICS THAT EXPIRE" and then discard all the lines that came before it. Output the remainder to a new file. Operating system is hp-ux. I've searched for usual awk and sed one liners but can't find a solution. Thank... (4 Replies)
Discussion started by: Scottie1954
4 Replies

8. Shell Programming and Scripting

awk to find pattern and add lines

My file goes like this: SID_LIST_HOSTNAME_LISTENER_3 = (SID_LIST = (SID_DESC = (SID_NAME = ORA0008) (ORACLE_HOME = /opt/oracle/product/ORA0008) (ENVS = "LD_LIBRARY_PATH=/opt/oracle/product/ORA0008/lib") ) (SID_DESC = (SID_NAME = ORA0007) ... (4 Replies)
Discussion started by: jpsingh
4 Replies

9. Shell Programming and Scripting

pattern matching lines using the date, and then joining the lines

Hi Guys, Was trying to attempt the below using awk and sed, have no luck so far, so any help would be appreciated. Current Text File: The first line has got an "\n", and the second line has got spaces/tabs then the word and "\n" TIME SERVER/CLIENT TEXT... (6 Replies)
Discussion started by: eo29
6 Replies

10. Shell Programming and Scripting

Find pattern, and then last field from subsequent lines

I've got a log file, of the format Name: network1 Dropped packets: 15618 Dropped packets for IPv6: 27 Dropped packets: 74 Dropped packets for IPv6: 0 Failed RADIUS Authentication procedures: 0 Failed RADIUS Accounting procedures: 0 Name: network2 Dropped packets: 1117 ... (18 Replies)
Discussion started by: Yorkie99
18 Replies
Login or Register to Ask a Question