Answer for this sed question ??
+2
Christopher
anand
6 posters
Page 1 of 2 • 1, 2
Answer for this sed question ??
tell me sed command which replaces "pink" with "blue" only for lines that contain the word "baby"?
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: Answer for this sed question ??
try this:
sed '/baby/s/pink/blue/g' filename
sed '/baby/s/pink/blue/g' filename
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: Answer for this sed question ??
yea both works...
thanks chris n akalya..........
thanks chris n akalya..........
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: Answer for this sed question ??
@akalya
in that format is that /baby/ part called the key?
in that format is that /baby/ part called the key?
jeeva- Posts : 50
Points : 93
Join date : 2010-03-04
Re: Answer for this sed question ??
@anand
if u want other contents too, try wat akalya sait. it works well.
if u want other contents too, try wat akalya sait. it works well.
Re: Answer for this sed question ??
@ akalya :
can u explain how it works ....
can u explain how it works ....
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: Answer for this sed question ??
it ll first search for lines that contains 'baby' and then substitutes pink with blue.
if u want to check for more than one words. Use as following
Here it ll search for line that contains both baby and father in order and replaces pink with blue.
if u want to check for more than one words. Use as following
- Code:
$ sed '/'baby' 'father'/s/pink/blue/g' filename
Here it ll search for line that contains both baby and father in order and replaces pink with blue.
Re: Answer for this sed question ??
yeah it works....chris
i have one...which ll replace lines which has "baby" or "father"
sed '/[father,baby]/s/pink/blue/g' 1.txt
i have one...which ll replace lines which has "baby" or "father"
sed '/[father,baby]/s/pink/blue/g' 1.txt
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: Answer for this sed question ??
it ll replace the lines which contains either baby or father
sorry guys... it s not working. but not able to find wat happened. ll reply soon.
sorry guys... it s not working. but not able to find wat happened. ll reply soon.
Last edited by Christopher on Tue Mar 09, 2010 4:49 pm; edited 1 time in total
Re: Answer for this sed question ??
@anand
i think what you said replaces pink with blue even in the lines where baby or father is not there
i think what you said replaces pink with blue even in the lines where baby or father is not there
jeeva- Posts : 50
Points : 93
Join date : 2010-03-04
Re: Answer for this sed question ??
actually this line
sed '/[baby,good]/s/pink/blue/g' 1.txt
replaces all lines which has the characters inside square brackets
sed '/[baby,good]/s/pink/blue/g' 1.txt
replaces all lines which has the characters inside square brackets
anand- Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36
Re: Answer for this sed question ??
@jeeva
according to anand stmt it ll replace all lines which contains any one of the character in "father" or "baby". if u want to test it, append one more line without any characters which were in 'father' or 'baby' and execute the command. u can notice the difference.
according to anand stmt it ll replace all lines which contains any one of the character in "father" or "baby". if u want to test it, append one more line without any characters which were in 'father' or 'baby' and execute the command. u can notice the difference.
Re: Answer for this sed question ??
@christopher
sed '/[father][baby]/s/pink/blue/g' 1.txt
sed '/[father,baby]/s/pink/blue/g' 1.txt
both work...wat abt the following
sed '/'baby' 'father'/s/pink/blue/g' 1.txt
does it work???
sed '/[father][baby]/s/pink/blue/g' 1.txt
sed '/[father,baby]/s/pink/blue/g' 1.txt
both work...wat abt the following
sed '/'baby' 'father'/s/pink/blue/g' 1.txt
does it work???
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: Answer for this sed question ??
hey akalya. it s not working. wat i said is not correct.
why 'pink' in line 2 changes??
- Code:
$ cat 2.txt
father pink powder
pink box table
colour colour colour
pink bottle baby
father pink baby
pink pink pink
$ sed '/[father][baby]/s/pink/blue/g' 2.txt
father blue powder
blue box table
colour colour colour
blue bottle baby
father blue baby
pink pink pink
why 'pink' in line 2 changes??
Re: Answer for this sed question ??
@christopher
ya its not working
mite b i din try with sufficient inputs..sry
but guess this wud work
combining expressions using -e
sed -e '/father/s/pink/blue/g' -e '/baby/s/pink/blue/g' 2.txt
ya its not working
mite b i din try with sufficient inputs..sry
but guess this wud work
combining expressions using -e
sed -e '/father/s/pink/blue/g' -e '/baby/s/pink/blue/g' 2.txt
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: Answer for this sed question ??
yes. we can use go for searching multiple patterns using -e switch. But wat is the significance of these stmts??
sed '/[father][baby]/s/pink/blue/g' 1.txt
sed '/[father,baby]/s/pink/blue/g' 1.txt
sed '/[father][baby]/s/pink/blue/g' 1.txt
sed '/[father,baby]/s/pink/blue/g' 1.txt
Re: Answer for this sed question ??
guess it replaces even if identifies a sub-pattern of the keywords given within the []brackets
when i gave
sed '/[father][taxi]/s/pink/blue/g' 2.txt
it still changed the pink in 2nd line to blue "ta"xi and "ta"ble
in ur case b"ab"y and t"ab"le
$ cat 2.txt
father pink powder
pink box table
colour colour colour
pink bottle baby
father pink baby
pink pink pink
$ sed '/[father][taxi]/s/pink/blue/g' 2.txt
father blue powder
blue box table
colour colour colour
blue bottle baby
father blue baby
pink pink pink
now,
"fa"ther and "fa"lour
$ cat 2.txt
father pink powder
pink box table
falour colour pink
pink bottle baby
father pink baby
pink pink pink
$ sed '/[father][baby]/s/pink/blue/g' 2.txt
father blue powder
blue box table
falour colour blue
blue bottle baby
father blue baby
pink pink pink
nyways i'm not sure .....
when i gave
sed '/[father][taxi]/s/pink/blue/g' 2.txt
it still changed the pink in 2nd line to blue "ta"xi and "ta"ble
in ur case b"ab"y and t"ab"le
$ cat 2.txt
father pink powder
pink box table
colour colour colour
pink bottle baby
father pink baby
pink pink pink
$ sed '/[father][taxi]/s/pink/blue/g' 2.txt
father blue powder
blue box table
colour colour colour
blue bottle baby
father blue baby
pink pink pink
now,
"fa"ther and "fa"lour
$ cat 2.txt
father pink powder
pink box table
falour colour pink
pink bottle baby
father pink baby
pink pink pink
$ sed '/[father][baby]/s/pink/blue/g' 2.txt
father blue powder
blue box table
falour colour blue
blue bottle baby
father blue baby
pink pink pink
nyways i'm not sure .....
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: Answer for this sed question ??
even a single character is a substring. So we can say that if it contains any one of the characters, it ll replace. Anyway it s not the case here. ll post another example.
Last edited by Christopher on Tue Mar 09, 2010 7:07 pm; edited 1 time in total
Re: Answer for this sed question ??
- Code:
$ cat 1.txt
pink father baby
make computer pink
baby with pink cap
pink pink pink
good pink colour
father pink box
$ sed '/[father][baby]/s/pink/blue/g' 1.txt
blue father baby
make computer pink
baby with blue cap
pink pink pink
good pink colour
father blue box
fath"er" ----> comput"er", but pink is not replaced.
Re: Answer for this sed question ??
tat's not the answer...
look at this
$ sed '/[cite][make]/s/pink/blue/g' 2.txt
father pink powder
blue box table
colour colour colour
pink bottle baby
father pink baby
pink pink pink
still line2 changes
y??????????
look at this
$ sed '/[cite][make]/s/pink/blue/g' 2.txt
father pink powder
blue box table
colour colour colour
pink bottle baby
father pink baby
pink pink pink
still line2 changes
y??????????
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Page 1 of 2 • 1, 2
Permissions in this forum:
You cannot reply to topics in this forum
|
|