Unix C++
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Answer for this sed question ??

+2
Christopher
anand
6 posters

Page 1 of 2 1, 2  Next

Go down

Answer for this sed question  ?? Empty Answer for this sed question ??

Post by anand Tue Mar 09, 2010 3:51 pm

tell me sed command which replaces "pink" with "blue" only for lines that contain the word "baby"?
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 3:56 pm

Try this command

Code:
grep 'baby' 1.txt | sed 's/pink/blue/g'
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by akalya Tue Mar 09, 2010 4:02 pm

try this:

sed '/baby/s/pink/blue/g' filename

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by anand Tue Mar 09, 2010 4:04 pm

yea both works...
thanks chris n akalya..........
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by jeeva Tue Mar 09, 2010 4:06 pm

@akalya

in that format is that /baby/ part called the key?

jeeva

Posts : 50
Points : 93
Join date : 2010-03-04

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 4:06 pm

@anand
if u want other contents too, try wat akalya sait. it works well.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by anand Tue Mar 09, 2010 4:09 pm

@ akalya :
can u explain how it works ....
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 4:18 pm

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

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.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by anand Tue Mar 09, 2010 4:23 pm

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
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 4:27 pm

No.. it s not working....
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 4:30 pm

try this command

Code:
$ sed '/[father][baby]/s/pink/blue/g' 1.txt
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 4:30 pm

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.


Last edited by Christopher on Tue Mar 09, 2010 4:49 pm; edited 1 time in total
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by jeeva Tue Mar 09, 2010 4:35 pm

@anand
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

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by anand Tue Mar 09, 2010 4:43 pm

actually this line

sed '/[baby,good]/s/pink/blue/g' 1.txt
replaces all lines which has the characters inside square brackets
anand
anand

Posts : 55
Points : 70
Join date : 2010-02-26
Age : 36

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 4:44 pm

@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.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by jeeva Tue Mar 09, 2010 4:48 pm

oh ok Smile

jeeva

Posts : 50
Points : 93
Join date : 2010-03-04

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by akalya Tue Mar 09, 2010 4:58 pm

@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???

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 6:02 pm

hey akalya. it s not working. wat i said is not correct.

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??
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 6:36 pm

anyone able to find what is happening???
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by akalya Tue Mar 09, 2010 6:39 pm

@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

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 6:53 pm

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
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by akalya Tue Mar 09, 2010 6:57 pm

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 .....

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 7:04 pm

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. Smile


Last edited by Christopher on Tue Mar 09, 2010 7:07 pm; edited 1 time in total
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Christopher Tue Mar 09, 2010 7:06 pm

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.
Christopher
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 35

https://unixcpp.forumotion.com

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by akalya Tue Mar 09, 2010 7:06 pm

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??????????

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

Back to top Go down

Answer for this sed question  ?? Empty Re: Answer for this sed question ??

Post by Sponsored content


Sponsored content


Back to top Go down

Page 1 of 2 1, 2  Next

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum