LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris, OpenSolaris, OpenIndiana, and illumos.
General Sun, SunOS and Sparc related questions also go here. Any Solaris fork or distribution is welcome.

Notices


Reply
  Search this Thread
Old 08-07-2013, 03:50 AM   #1
sras
Member
 
Registered: Jul 2013
Posts: 31

Rep: Reputation: Disabled
Replace nth line after the pattern match


Hello Guys,

How to replace the nth line after the pattern match in a file.

Example:

I am searching for the pattern "queue" from the content below, once it is found, I need to replace the 3rd line from there..


<bean id="queue" class="java_class_name">
<property name="Template" ref="jndiTemplate"/>
<property name="jndiName" value="Old_value"/>
</bean>

thanks.
 
Old 08-07-2013, 04:41 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399
Can you tell us a bit more.

- I assume you want to change/replace the <property name="jndiName" value="Old_value"/> line. Correct?
- Change/replace with what? Is the complete line replaced or just one (or more) of its attributes?

Please provide a relevant input and wanted output example.
 
Old 08-07-2013, 04:42 AM   #3
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233
See if that works for you:

Code:
sed '/queue/ {n;n;a\
Text to be put in the third line
}' file.txt
This replaces the third line with whatever you put in:
Quote:
Text to be put in the third line
If you want to replace the fifth line, just add 2 more 'n' characters in the command.

To make the changes permanent, either output the command to a new file with

Code:
}' file.txt > new_file.txt
Or use the -i flag
 
Old 08-07-2013, 04:54 AM   #4
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
as always you guys are faster than me on such questions, but the following awk script should also do the trick:

Code:
BEGIN {COUNTER=-1;} 
/.*queue.*/ {COUNTER=3;} 
/.*/{
	if (COUNTER == 0) 
	{
		COUNTER--;
		print "my_replacement";
	}
	else if (COUNTER>0) 
	{
		COUNTER--;
		print $0;  
	}
	else
	{
		print $0;
	}
}
The second line is where you can set how many lines you want to count downward...

Run the script with e.g.
Code:
awk -f /path/to/script /path/to/file/you/want/to/process
 
Old 08-07-2013, 04:57 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399
@joe_2000: This is done on solaris, so you might need to use /usr/xpg4/bin/awk instead of the regular (n)awk.

@sycamorex: Happy B-Day!
 
1 members found this post helpful.
Old 08-07-2013, 04:57 AM   #6
sras
Member
 
Registered: Jul 2013
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Can you tell us a bit more.

- I assume you want to change/replace the <property name="jndiName" value="Old_value"/> line. Correct?
- Change/replace with what? Is the complete line replaced or just one (or more) of its attributes?

Please provide a relevant input and wanted output example.

Thanks, Druuna.

I need to change the one attribute (value) in the line.
The ouput should be:

<bean id="queue" class="java_class_name">
<property name="Template" ref="jndiTemplate"/>
<property name="jndiName" value="NEW_VALUE"/>
</bean>
 
Old 08-07-2013, 05:00 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399Reputation: 2399
Have a look at this:
Code:
sed '/bean id="queue"/{N;N;s/Old_value/XXXX/}' infile
XXXX being the new value.

BTW: Not sure if the sed that comes with solaris can do this, you might want to use an alternative gnu sed (if installed).
 
Old 08-07-2013, 05:18 AM   #8
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233Reputation: 1233
Quote:
Originally Posted by druuna View Post
@joe_2000: This is done on solaris, so you might need to use /usr/xpg4/bin/awk instead of the regular (n)awk.

@sycamorex: Happy B-Day!
Thank you.
 
Old 08-07-2013, 06:03 AM   #9
sras
Member
 
Registered: Jul 2013
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by joe_2000 View Post
as always you guys are faster than me on such questions, but the following awk script should also do the trick:

Code:
BEGIN {COUNTER=-1;} 
/.*queue.*/ {COUNTER=3;} 
/.*/{
	if (COUNTER == 0) 
	{
		COUNTER--;
		print "my_replacement";
	}
	else if (COUNTER>0) 
	{
		COUNTER--;
		print $0;  
	}
	else
	{
		print $0;
	}
}
The second line is where you can set how many lines you want to count downward...

Run the script with e.g.
Code:
awk -f /path/to/script /path/to/file/you/want/to/process

Thanks, Joe. It worked exactly with /usr/xpg4/bin/awk -f.
Could you explain what is the meaning of third line in the script(/.*/).
 
Old 08-07-2013, 06:12 AM   #10
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 307Reputation: 307Reputation: 307Reputation: 307
Quote:
Originally Posted by sras View Post
Thanks, Joe. It worked exactly with /usr/xpg4/bin/awk -f.
Could you explain what is the meaning of third line in the script(/.*/).
Sure - this just means that the following statement will be applied to all lines of the input file, as opposed to only the one that contains the string "queue", where I am setting the counter to 3.

Glad it worked for you...

Quote:
Originally Posted by druuna View Post
@joe_2000: This is done on solaris, so you might need to use /usr/xpg4/bin/awk instead of the regular (n)awk.
Thanks for pointing that out, I was not aware of this.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace the space with newline character after every nth field in a line surajchalukya Linux - Newbie 13 03-08-2013 09:58 PM
Delete all Characters in Line After Pattern Match dbol Programming 4 01-29-2013 10:37 AM
Match pattern and replace sol_nov Programming 7 11-30-2009 08:23 PM
Perl: Match part of a line and replace with another line from the same file briana.paige Linux - Newbie 8 06-27-2009 06:35 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM

LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris

All times are GMT -5. The time now is 06:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration