Sed/awk/perl command to replace pattern in multiple lines | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Sed/awk/perl command to replace pattern in multiple lines

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-20-2012
dani777 dani777 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 21 December 2012, 10:42 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Sed/awk/perl command to replace pattern in multiple lines

Hi
I know sed and awk has options to give range of line numbers, but
I need to replace pattern in specific lines
Something like

Code:
sed -e '1s,14s,26s/pattern/new pattern/' file name

Can somebody help me in this....

I am fine with see/awk/perl
Thank you in advance

Last edited by Franklin52; 12-21-2012 at 02:47 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old 12-20-2012
spacebar's Avatar
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 15 April 2014, 9:00 PM EDT
Location: spaceBAR Central
Posts: 434
Thanks: 0
Thanked 68 Times in 68 Posts
Try it using this format:

Code:
sed 'beginning_line#,ending_line# s/pattern/new pattern/' file_name

Sponsored Links
    #3  
Old 12-21-2012
sathyaonnuix's Avatar
sathyaonnuix sathyaonnuix is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 4 December 2013, 2:33 AM EST
Posts: 127
Thanks: 34
Thanked 14 Times in 13 Posts
Have a look on this thread.


Code:
# nl file
     1  Sathya
     2  Sathya
     3  Sathya
     4  Sathya
     5  SAthya
     6  Narayanan
     7  Narayanan
     8  Sathya
     9  Sathya
    10  Narayanan


Code:
# sed -e "1s/Sathya/XXX/" -e "3s/Sathya/XXX/" -e "9s/Sathya/XXX/" file
XXX
Sathya
XXX
Sathya
SAthya
Narayanan
Narayanan
Sathya
XXX
Narayanan


Last edited by sathyaonnuix; 12-21-2012 at 12:23 AM..
    #4  
Old 12-21-2012
Kashyap Kashyap is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 15 October 2013, 5:41 AM EDT
Posts: 24
Thanks: 9
Thanked 0 Times in 0 Posts
Hi Sathyaonunix

Dear sathyaonunix,
Consider that the pattern that you have changed are different ie., say
XXX1
XXX2
XXX3

and these might be the contents of a file say file2.txt
now can you please help me to replace the pattern Sathya in file1 with the three patterns of file2.txt.

And what if the contents of file1 that contains
nl file 1 Sathya 2 Sathya 3 Sathya 4 Sathya 5 SAthya 6 Narayanan 7 Narayanan 8 Sathya 9 Sathya 10 Narayanan might not have a key and might be arranged in a singel line like this,
sathya sathya sathya sathya sathya Narayanan Narayanan Sathya sathya Narayanan

Please suggest me how to replace if we encounter such a problem,

Thanks,
Kashyap.



Thanks,
Kashyap.
Sponsored Links
    #5  
Old 12-21-2012
sathyaonnuix's Avatar
sathyaonnuix sathyaonnuix is offline
Registered User
 
Join Date: Aug 2012
Last Activity: 4 December 2013, 2:33 AM EST
Posts: 127
Thanks: 34
Thanked 14 Times in 13 Posts
For your second query, here is the solution :


Code:
# cat file
sathya sathya sathya sathya sathya Narayanan Narayanan Sathya sathya Narayanan

# sed 's/ /\n/g' file
sathya
sathya
sathya
sathya
sathya
Narayanan
Narayanan
Sathya
sathya
Narayanan

For the first one can you please explain more on how you want to achieve it.

---------- Post updated at 09:05 AM ---------- Previous update was at 04:35 AM ----------

If I understood your question correctly here is the code:

Code:
# cat file1
sathya
sathya
sathya
sathya
sathya
Narayanan
Narayanan
Sathya
sathya
Narayanan

# cat file2
XXX1
XXX2
XXX3

# sed -e "1s/sathya/$(sed -n '1p' file2)/" -e "3s/sathya/$(sed -n '2p' file2)/" -e "9s/sathya/$( sed -n '3p' file2)/" file1
XXX1
sathya
XXX2
sathya
sathya
Narayanan
Narayanan
Sathya
XXX3
Narayanan

Sponsored Links
    #6  
Old 12-21-2012
dani777 dani777 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 21 December 2012, 10:42 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Sathyaonunix,
I think your suggestion should work for me.Thank You...
Sponsored Links
    #7  
Old 12-21-2012
MadeInGermany MadeInGermany is offline Forum Advisor  
Registered User
 
Join Date: May 2012
Last Activity: 24 April 2014, 7:34 PM EDT
Posts: 1,613
Thanks: 90
Thanked 457 Times in 414 Posts

Code:
sed -e '1b' -e '14b' -e '26b' -e 's/pattern/replacement/'

GNU sed takes

Code:
sed '1b; 14b; 26b; s/pattern/replacement/'

---------- Post updated at 11:33 AM ---------- Previous update was at 11:19 AM ----------

...and the reverse is:

Code:
sed -e '1bx' -e '14bx' -e '26bx' -e 'b' -e ':x' -e 's/pattern/replacement/'

Maybe this is better readable:

Code:
sed '
1bx
14bx
26x
b
:x
s/pattern/replacement/
'

---------- Post updated at 11:44 AM ---------- Previous update was at 11:33 AM ----------

Last but not least:

Code:
awk 'NR==1 || NR==14 || NR==26 {sub("pattern","replacement")} {print}'

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed replace using same pattern repeating multiple times in a line sol_nov Shell Programming and Scripting 4 10-15-2012 09:01 PM
Command to remove duplicate lines with perl,sed,awk cola Shell Programming and Scripting 9 10-19-2010 04:57 AM
awk/sed/perl command to delete specific pattern and content above it... edge_diners Shell Programming and Scripting 7 07-05-2010 04:43 AM
sed/awk to insert multiple lines before pattern zksailor534 Shell Programming and Scripting 2 12-02-2009 02:21 PM
using sed command to replace multiple lines radha.kalivar Shell Programming and Scripting 1 07-10-2007 10:36 AM



All times are GMT -4. The time now is 05:15 AM.