Merge lines till a particular word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge lines till a particular word
# 1  
Old 09-12-2014
Merge lines till a particular word

Hi Experts,

I have a requirement like, I have to search between 2 words (<deviceDetails> and </deviceDetails>) and merge all lines in between into 1 line.
Example:
Code:
<deviceDetails><subscriberName>#UNKNOWN#</subscriberName>
<customerNumber>#UNKNOWN#</customerNumber>
<MCN>#UNKNOWN#</MCN></deviceDetails>
<deviceDetails><subscriberName>ABC</subscriberName>
<customerNumber>123</customerNumber>
<MCN>#UNKNOWN#</MCN></deviceDetails>

Output (2 lines):
Code:
<deviceDetails><subscriberName>#UNKNOWN#</subscriberName><customerNumber>#UNKNOWN#</customerNumber><MCN>#UNKNOWN#</MCN></deviceDetails>
<deviceDetails><subscriberName>ABC</subscriberName><customerNumber>123</customerNumber><MCN>#UNKNOWN#</MCN></deviceDetails>

Thanks in advance.
# 2  
Old 09-12-2014
Code:
$ awk -v ORS="" '/\/deviceDetails/ { $0=$0"\n" } 1' < infile > outfile

$ cat outfile

<deviceDetails><subscriberName>#UNKNOWN#</subscriberName><customerNumber>#UNKNOWN#</customerNumber><MCN>#UNKNOWN#</MCN></deviceDetails>
<deviceDetails><subscriberName>ABC</subscriberName><customerNumber>123</customerNumber><MCN>#UNKNOWN#</MCN></deviceDetails>

$

If your real input is much different, this may not work.
# 3  
Old 09-12-2014
Hi,
Thanks for quick response.. Yes my actual input is quite big sometime it splitted into 50 lines and sometime its 10 lines only. The above command is giving correct o/p for the above example but its not working for the actual data..
# 4  
Old 09-12-2014
Then post a sample of the actual input here...
# 5  
Old 09-12-2014
Try:
Code:
awk '$1==s{f=1} $1=="/" s{f=0} f{gsub(/\n/,x)} NR>1{print RS $0}' RS=\< FS=\> ORS= s=deviceDetails file


Last edited by Scrutinizer; 09-12-2014 at 05:54 PM..
# 6  
Old 09-12-2014
Quote:
Originally Posted by satyaatcgi
Hi,
Thanks for quick response.. Yes my actual input is quite big sometime it splitted into 50 lines and sometime its 10 lines only. The above command is giving correct o/p for the above example but its not working for the actual data..
Then post real data, edited for confidentiality if necessary. There's no point testing on a cleaned-up mockup.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge mutiple lines into one based on if the first word is some particular value

Hi, trying to knock something together to create one line entries based on whether the first word on each line matches a particular value. eg. Link,"Name=""Something\something"","Timeout=""1800""", "Target=""\\thing\thing\thing""","State=""ONLINE""",something,... (0 Replies)
Discussion started by: adamdb
0 Replies

2. UNIX for Dummies Questions & Answers

Shell Script for displaying the line till the target word

" Script for display sentences with special character" Hi, Could any one share a command how to display a line until my target word. For ex: My file has the content as: select * from db_wrk where col1 < col2 insert into table_name values('1','2','tst','wrk','dev','prod') My target... (10 Replies)
Discussion started by: Kalaiselvi66
10 Replies

3. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: &quot;Liberation Sans&quot;; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

4. UNIX Desktop Questions & Answers

Merge Last Word to Another File

Hi, Im a newbie to scripting. I have a file that looks like the one below. How can i change the last "/" to become a space. Thank You so Much for the help. :) hostname date Feb-9 /u100/DEVCO/Patching/a.log hostname date Jun-25 /u100/DEVCO/DumpCleaner/a.log hostname date Jun-25... (2 Replies)
Discussion started by: lienyca
2 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Shell Programming and Scripting

Get the number of lines till I get line

Hi All, I have a file as below: abc.txt ****************************** * HEADER DESCRIPTION ****************************** *Supplier: Prism Customer: MNI -NIGERIA Quantity: 2 Type: PLUG-IN Profile: 70.00 *Subscription: Generic... (5 Replies)
Discussion started by: arunshankar.c
5 Replies

7. Shell Programming and Scripting

Search the word to be deleted and delete lines above this word starting from P1 to P3

Hi, I have to search a word in a text file and then I have to delete lines above from the word searched . For eg suppose the file is like this: Records P1 10,23423432 ,77:1 ,234:2 P2 10,9089004 ,77:1 ,234:2 ,87:123 ,9898:2 P3 456456 P1 :123,456456546 P2 abc:324234 (2 Replies)
Discussion started by: vsachan
2 Replies

8. Shell Programming and Scripting

Bash take word after specific point and till next space?

Hello, I have an output like Interface Chipset Driver wlan0 Intel 4965/5xxx iwlagn - and I want to take only the 'wlan0' string. This can be done by a="Interface Chipset Driver wlan0 Intel 4965/5xxx iwlagn - " b=${a:25:6} echo $bThe thing is that wlan0 can be something else, like eth0 or... (2 Replies)
Discussion started by: hakermania
2 Replies

9. Shell Programming and Scripting

How to print lines till till a pattern is matched in loop

Dear All I have a file like this 112534554 446538656 444695656 225696966 226569744 228787874 113536566 443533535 222564552 115464656 225445345 225533234 I want to cut the file into different parts where the first two columns are '11' . The first two columns will be either... (3 Replies)
Discussion started by: anoopvraj
3 Replies

10. UNIX for Dummies Questions & Answers

Read lines till a blank line is encountered

Hi, I have reached at a specified offset from the start of file. My requirement is that I want to read only those lines, which have the string READ / ALTER / UPDATE. As soon as, none of these literals are found in the subsequent line, I want to stop reading. Is there any feature of grep which... (1 Reply)
Discussion started by: saurabhsinha23
1 Replies
Login or Register to Ask a Question