Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed/awk/perl command to replace pattern in multiple lines
# 1  
Old 12-21-2012
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 03:47 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 12-21-2012
Try it using this format:
Code:
sed 'beginning_line#,ending_line# s/pattern/new pattern/' file_name

# 3  
Old 12-21-2012
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 01:23 AM..
# 4  
Old 12-21-2012
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.
# 5  
Old 12-21-2012
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

# 6  
Old 12-21-2012
Sathyaonunix,
I think your suggestion should work for me.Thank You...
# 7  
Old 12-21-2012
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}'


Previous Thread | Next Thread
Test Your Knowledge in Computers #518
Difficulty: Medium
Neither integer nor floating point numbers can represent every possible number since they use a finite number of bits to represent the number.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/awk/perl substitution with multiple lines

OSX I have been grinding my teeth on a portion of code. I am building a bash script that edits a html email template. In the template, I have place holders for SED (or whatever program is appropriate) to use as anchors for find and replace, with user defined corresponding html code. The HTML code... (3 Replies)
Discussion started by: sudo
3 Replies

2. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

4. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

5. Shell Programming and Scripting

Replace multiple lines through sed

Hi All, I have a input file as sample below <this is not starting of file> record line1 line2 line3 end line4 line5 record line6 line7 line8 my requirement is this, i want to select a pattern between first record and end, whatever is written between first record and end. and... (0 Replies)
Discussion started by: adgangwar
0 Replies

6. Shell Programming and Scripting

Command to remove duplicate lines with perl,sed,awk

Input: hello hello hello hello monkey donkey hello hello drink dance drink Output should be: hello hello monkey donkey drink dance (9 Replies)
Discussion started by: cola
9 Replies

7. Shell Programming and Scripting

awk/sed/perl command to delete specific pattern and content above it...

Hi, Below is my input file: Data: 1 Length: 20 Got result. Data: 2 Length: 30 No result. Data: 3 Length: 20 (7 Replies)
Discussion started by: edge_diners
7 Replies

8. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies

9. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

10. Shell Programming and Scripting

using sed command to replace multiple lines

the file contains the follwoing lines /* * Copyright (C) 1995-1996 by XXX Corporation. This program * contains proprietary and confidential information. All rights reserved * except as may be permitted by prior written consent. * * $Id: xxx_err.h,v 1.10 2001/07/26 18:48:34 zzzz $ ... (1 Reply)
Discussion started by: radha.kalivar
1 Replies

Featured Tech Videos