sed command - substitue first instance

hi

i have one file where i want to substitute only first instance of
swap
with swap1
i want to replcae only first instance of swap in my script

i know we can do this with awk. but i need to do this with sed only

i tried follwoing code
sed 's/swap/swap1' filename

but here all swap will get replaced with swap1

i want to replace only one and first instance of swap

plz guide me

Thank You
Best Regards,
Swapneel

This sounds like homework. Why not do it in awk?

You have syntax problem here ..
sed 's/swap/swap1' filename

Change as

sed 's/swap/swap1/' filename

hi,
see i m able to use sed command. i dont have any sytex errores. might be while typing here i missed out something.

see i m using sed command but i want to replace only first /or nth instance in file. i dont want replace all instances .

i repeat only first instance.

And guys thisis not home work. plz reply with solutions.

thanks
Swapneel

sed -e '1,/pattern/s/pattern/REPLACEMENT/' <INPUTFILE

dont ask me why it works...cause that will sound like homework....just figure it out :slight_smile:
regards

hi JunkYardWars

sed -e '1,/pattern/s/pattern/REPLACEMENT/' <INPUTFILE

it doesnot work. it will replace all instances of specified pattern.
i want to replace only first instance not the all

anyway thanks for your time.

Regards
swapneel

sed -e 's/pattern/REPLACEMENT/1' <INPUTFILE

hi vgersh99

sed -e 's/pattern/REPLACEMENT/1' <INPUTFILE

i tried your code already. but it does not work.
can u test it on your side once? please.

and one more thing
is it possible to replace first instance of search patternt by using sed command???

is it possible to replcae first insatnce by using sed command?
if its not possible we will close this thread.. instead of [w]/investing our time.

i tried with this one and it didnot work,

>cat filename
swap
swap1
swap2
swap
swap

ur command

sed -e 's/swap/newswap/1' < filename

and this is the output of ur command

newswap
newswap1
newswap2
newswap
newswap

try this command,

awk 'BEGIN{x=0} /swap/ {if (x==0) { print "replace"; x=1} else { print } }' filename

and this is the output,

replace
swap1
swap2
swap
swap

Is this what you were expecting?

hi matrixmadhan

thanks for your reply and time.
and thanks for your awk solution.

but one quetion
is it possible bu using sed?
if u refer man pages then its clearly mentioned -- by specifying flags we can replace nth instance.

so just see is it possible by using sed.

Thanks again
Regards,
swapneel

sed's s command operates on the pattern space. By default sed will load one line at a time into the pattern space. So by specifying a flag "1", you ask sed the change the first occurence on each line. With complex sed scripts you can arrange for several lines to be in the pattern buffer at once. But if you try to load the entire file into the pattern buffer and then execute a single s command, you are limiting yourself to small files only. A general sed solution:

sed '-e/swap/{;s/swap/swap1/;:a' '-en;ba' '-e}' <filename

hi Perderabo

ya its working fine. but frankly i m not able to understand
-en;ba parameters...

sed '-e/swap/{;s/swap/swap1/;:a' '-en;ba' '-e}' <filename

could you plz explain us its execution in short.
as i m struggling with this since long time.

thanks a lot
regards,
swapneel

The /swap/{....} will run the program in the braces when a line with "swap" is encountered. The program replaces swap with swap1. Only the first instance on the line is replaced, no 1 flag is needed since that is the default. Then there is an infinite loop reading lines and printing them out. This uses up the rest of the file and sed will automatically terminate the infinite loop.

I have a list of IPs, and I want to delete just '10.0.0.1'. I would like a sed/awk solution that will only delete '10.0.0.1' and not modify all the IPs
10.0.0.1
10.0.0.2
10.0.0.3
10.0.0.4
10.0.0.5
10.0.0.6
10.0.0.7
10.0.0.8
10.0.0.9
10.0.0.10
10.0.0.11
Tried:

sed -e '/10.0.0.1/d' file
sed '/10.0.0.1/d' file
sed 's/10.0.0.1//1' file

but they all modify the list by removing all the instances of 10.0.0.1 out of the list so I'm only left with
2
3
4
5
6
7
8
9
0
1
Any suggestions?

sed '-e/10.0.0.1/{;s/10.0.0.1//;:a' '-en;ba' '-e}' <file

Works like a champ. Thank you.

You will need to escape the . metacharacter. Otherwise that will mean any character

sed '/^10\.0\.0\.1$/d' inputfile > outfile

cheers,
Devaraj Takhellambam