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

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

Try it using this format:

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

Have a look on this thread.

# nl file
     1  Sathya
     2  Sathya
     3  Sathya
     4  Sathya
     5  SAthya
     6  Narayanan
     7  Narayanan
     8  Sathya
     9  Sathya
    10  Narayanan
# sed -e "1s/Sathya/XXX/" -e "3s/Sathya/XXX/" -e "9s/Sathya/XXX/" file
XXX
Sathya
XXX
Sathya
SAthya
Narayanan
Narayanan
Sathya
XXX
Narayanan

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.

For your second query, here is the solution :

# 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:

# 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

Sathyaonunix,
I think your suggestion should work for me.Thank You...

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

GNU sed takes

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

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

...and the reverse is:

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

Maybe this is better readable:

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:

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

Hi,

Yes the solution that you have mentioned for my specified problem is correct.
But if the position of sathya varies each time then how can we solve it???
Let me explain u the problem,\

kashyap kashyap manju suhas Narayanan Narayanan frank wall sathya 

So these are the contents of the file1, Now consider that initially sathya is in the last position,
But when u use the file the second time the postion might change and the new position of sathya
would be the second or third or fourth, So what i mean to say is irrespective of positions of the word sathya , i want to add the contents of file2 one after the other next to sathya.......

A demo piece would be like this...

sathya manju kashyap  Narayanan Narayanan Sathya sathya Narayanan  

SO the contents of file2 are

XYZ1
XYZ2
XYZ3

Now i want the output like this,

 sathya XYZ1 manju kashyap  Narayanan Narayanan Sathya XYZ2 sathya XYZ3 Narayanan 

The number of "sathya" in file1 will be equal to the strings present in file2.
I hope u have understood my problem.
I am trying to do this for the past 15 days and i am unable achieve it,
It would be very helpful if u solve this problem.

Thanks,
Kashyap.

Please try this
This may work:

# cat > file1
sathya manju kashyap Narayanan Narayanan Sathya sathya Narayanan

# sed -i 's/ /\n/g' file1

# cat file1
sathya
manju
kashyap
Narayanan
Narayanan
Sathya
sathya
Narayanan

# cat file2
XYZ1
XYZ2
XYZ3


for i in `cat file2` 
do
sed -i "0,/sathya/I {s/Sathya/$i/I}" file1
done

cat file1

sathya
manju
kashyap
Narayanan
Narayanan
Sathya
sathya
Narayanan

cat file2

XYZ1
XYZ2
XYZ3

If file2 fits into memory then you can read it into an array.

awk '
FILENAME=="-" {array[NR]=$1; next}
{print $1}
tolower($1)==x {print array[++pos]}
' x=sathya - file1 <file2

sathya
XYZ1
manju
kashyap
Narayanan
Narayanan
Sathya
XYZ2
sathya
XYZ3
Narayanan