Grep 2 consecutive lines and replace the second line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep 2 consecutive lines and replace the second line in a file
# 1  
Old 03-09-2015
Grep 2 consecutive lines and replace the second line in a file

I have a file lake this

cat ex1.txt



Code:
</DISCOUNTS>
		<B2B_SPECIFICATION elem="0">
			<B2B_SPECIFICATION elem="0">
				<DESCR>Netti 2 </DESCR>
				<NUMBER>D02021507505</NUMBER>
			</B2B_SPECIFICATION>
			<B2B_SPECIFICATION elem="1">
				<DESCR>Puhepaketti</DESCR>
				<NUMBER>D02021507505</NUMBER>
			</B2B_SPECIFICATION>
		</B2B_SPECIFICATION>
</SUBSCRIPTION_LABEL>

Hi

i want to grep 2 consecutive occurrence of <B2B_SPECIFICATION elem="0">
and first line should be replaced with <B2B_INFO>

like this

Code:
</DISCOUNTS>
		<B2B_INFO elem="0">
			<B2B_SPECIFICATION elem="0">
				<DESCR>Netti 2 </DESCR>
				<NUMBER>D02021507505</NUMBER>
			</B2B_SPECIFICATION>
			<B2B_SPECIFICATION elem="1">
				<DESCR>Puhepaketti</DESCR>
				<NUMBER>D02021507505</NUMBER>
			</B2B_SPECIFICATION>
		</B2B_SPECIFICATION>
</SUBSCRIPTION_LABEL>

# 2  
Old 03-09-2015
Hi,
Are you sure that it's sufficient ?
You don't need change close tag ?
Code:
<B2B_INFO elem="0">
...
...
</B2B_SPECIFICATION>

Regards.
# 3  
Old 03-09-2015
And, which one should be processed, the one from the title
Quote:
Grep 2 consecutive lines and replace the second line in a file
(and I doubt it's the 2. in file as there should be at least one with <DISCOUNTS>)
or the one from your spec
Quote:
and first line should be replaced
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple patterns(file) and replace whole line

I am able to grep multiple patterns which stored in a files. However, how could we replace the whole line with either the pattern or new string? For example: pattern_file: *Info in the () is not part of the pattern file. They are the intended name to replace the whole line after the pattern... (5 Replies)
Discussion started by: wxboo
5 Replies

2. AIX

Replace consecutive occurrence of string in same line

Hi All, I have a requirement to replace consecutive occurence of same string nedd to be replaced. Below is the input and desired output. Input: --------- 123.5|ABC|.|.|. 234.4|DEF|.|.|.|.|.| Output: --------- 123.5|ABC|||. 234.4|DEF||||| so basically "|.|" need to be replaced with... (9 Replies)
Discussion started by: ureddy
9 Replies

3. Shell Programming and Scripting

Grep three consecutive lines if each lines contains certain string

say we have : 2914 | REQUEST | whatever 2914 | RESPONSE | whatever 2914 | SUCCESS | whatever 2985 | RESPONSE | whatever 2986 | REQUEST | whatever 2990 | REQUEST | whatever 2985 | RESPONSE | whatever 2996 | REQUEST | whatever 2010 | SUCCESS | whatever 2013 | REQUEST | whatever 2013 |... (7 Replies)
Discussion started by: Saumitra Pandey
7 Replies

4. UNIX for Dummies Questions & Answers

Delete 26 consecutive lines in a file

I have a text file that is about 90,000 lines long. How would I delete lines 64-89, 152-177, 240-265, 328-353... etc? The sections I would like to delete are 26 lines long and the number of lines between the sections I would like to delete is 62 lines. Thanks very much in advance. (6 Replies)
Discussion started by: MDeBiasse
6 Replies

5. Shell Programming and Scripting

Grep couple of consecutive lines if each lines contains certain string

Hello, I want to extract from a file like : 20120530025502914 | REQUEST | whatever 20120530025502968 | RESPONSE | whatever 20120530025502985 | RESPONSE | whatever 20120530025502996 | REQUEST | whatever 20120530025503013 | REQUEST | whatever 20120530025503045 | RESPONSE | whatever I want... (14 Replies)
Discussion started by: black_fender
14 Replies

6. Shell Programming and Scripting

How to insert line with between two consecutive lines that match special pattern?

I have following pattern in a file: 00:01:38 UTC abcd 00:01:48 UTC 00:01:58 UTC efgh 00:02:08 UTC 00:02:18 UTC and I need to change something like the following 00:01:38 UTC abcd 00:01:48 UTC XXXX 00:01:58 UTC efgh 00:02:08 UTC XXXX (6 Replies)
Discussion started by: jjnight
6 Replies

7. Shell Programming and Scripting

sed to replace a line with 2 or more different lines in same file

i have something like this... cat filename.txt <complexType name="abc"> bklah vlah blah blha blha blah blha </complexType > <complexType name="def"> bklah vlah blah blha blha blah blha </complexType > . . .and so on.. its a very large file (11 Replies)
Discussion started by: vivek d r
11 Replies

8. Shell Programming and Scripting

Removing consecutive lines in a file

We have very large transaction logs that have transactions which start with a line that starts with 'Begin :' and ends with a line that starts with 'End :'. For most transactions there is valid data between those two lines. I am trying to get rid of lines that look like this: Begin :... (11 Replies)
Discussion started by: deneuve01
11 Replies

9. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

10. Shell Programming and Scripting

grab next consecutive line or lines

Hello i'm writting a krn shell that will find the letter F and I would like to grab everything from F till 0:00. Is there a sed or awk command that i could use. Thank you. File is looks like this. 11/12 xxx xxxx xxx F xxxx xxxx some info entered here some info entered here xxxx... (1 Reply)
Discussion started by: wisher115
1 Replies
Login or Register to Ask a Question