sed command

Hi.....
I'm using sed command for replace the words in a file
cat >test.txt
My test.txt contains

Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a lamb spamwich
Marry had a spicy wich
$ sed 's/wich$/mirchi/g' test.txt

output is:

Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandmirchi
Jill had a lamb spammirchi
Marry had a spicy mirchi

But I dont want to change combination of wich (i.e., sandwich & spamwich should not change)
plz give me correct command.......if any command other than sed plz send me...I'm waiting for ur reply

sed 's/ wich$/ mirchi/' test.txt
$ cat file
Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a lamb spamwich
wich Marry had a spicy wich

$ ruby -ne 'print $_.gsub(/\bwich\b/,"mirchi") ' file
Mary had a little ham
Mary fried a lot of spam
Jack ate a Spam sandwich
Jill had a lamb spamwich
mirchi Marry had a spicy mirchi

No yar any of command what u sent it those are not working plz help me out.

a small modification to bartus11's solution ::

that won't work if wich is in front

wich Marry had a spicy wich

oops i missed a test case :P.

Can we try using -e to append multiple commands.

sure.. you can take care of cases of multiple spaces/tabs as well, not just 1 space

Thanks SHeel and all... who are gave reply to my qstn......
But now Im facing another problem.....
If i want to change the data folder to newdata folder and newdata folder to data folder that same command is not working....
Actually I have path.txt file in that

$ cat path.txt
path="f:/sri/project/data/doc1
Datapath ="f:/sri/project/data/file2

so if i want to change data to newdata it's ok but after changing the path="f:/sri/project/newdata/doc1

Datapath="f:/sri/project/newdata/file2

sed  's/data/newdata/g' /home/sritest/path.txt
else
     sed  's/newdata/data/g' /home/sritest/path.txt
fi

It's not going to else part. in path.txt it again search for data(i.e., in newdata contains data ) there again its changing to newnewdata
that means
im getting following output

path="f:/sri/project/newnewdata/doc1
Datapath="f:/sri/project/newnewdata/file2

Howmany times i run that srcipt that much times its adding newdata into path.txt
but I dont want like this type of output....
I think u all Understood my problem pplz help me out.......

sed 's/ data/ newdata/g' path.txt

Its not working for changing the foldernames.plz giveme as soon as possible reply

try this..
this will help you:

sed 5's/wich$/mirchi/g' test.txt

where 5 is the line no.

regards
rajesh

Thanks rajesh But for folders its not working plz see the before I posted one more problem If u know soluton plz give me reply
onceagain thanks.......

Try GNU sed:

sed 's/\bwich\b/mirchi/g' infile

Regular sed:

sed 's/\([ \t]\)wich$/\1mirchi/;s/^wich\([ \t]\)/mirchi\1/;s/\([ \t]\)wich\([ \t]\)/\1mirchi\2/g'  infile

or

sed 's/^/ /;s/$/ /;s/\([ \t]\)wich\([ \t]\)/\1mirchi\2/g;s/^ //;s/ $//' infile

thanks a lot... who are giving reply to wich/mirchi for that query....
but I'm facing another problem plz see the before post... i'm not able to run for folders like data folder to newdata folder...plz see the above posted query...plz give me reply that query

If i want to change the data folder to newdata folder and newdata folder to data folder that same command is not working....
Actually I have path.txt file in that

$ cat path.txtpath="f:/sri/project/data/doc1Datapath ="f:/sri/project/data/file2

so if i want to change data to newdata it's ok but after changing the path="f:/sri/project/newdata/doc1

Datapath="f:/sri/project/newdata/file2
...
   sed 's/data/newdata/g' /home/sritest/path.txt
else
   sed 's/newdata/data/g' /home/sritest/path.txtfi
...

It's not going to else part. in path.txt it again search for data (i.e., in newdata contains data ) there again its changing to newnewdata that means im getting following output

path="f:/sri/project/newnewdata/doc1Datapath="f:/sri/project/newnewdata/file2

Howmany times i run that srcipt that much times its adding newdata into path.txt but I dont want like this type of output.... I think u all Understood my problem pplz help me out.......

sed 's/ data/ newdata/g' path.txt

Its not working for changing the foldernames.plz giveme as soon as possible reply

This seems to be very close related to another post of yours so merging both threads.

@ksrivani

sed 's/data/newdata/g' /home/sritest/path.txt

Note that this command will give you an output where the replacement has been done, BUT the input file /home/sritest/path.txt remain unchanged !
...not sure what you are trying to do with the "else" statement

$ echo "bla/data/bla bla/data/bla"
bla/data/bla bla/data/bla
$ echo "bla/data/bla bla/data/bla" | sed 's/data/newdata/'
bla/newdata/bla bla/data/bla
$ echo "bla/data/bla bla/data/bla" | sed 's/data/newdata/2'
bla/data/bla bla/newdata/bla
$ echo "bla/data/bla bla/data/bla" | sed 's/data/newdata/g'
bla/newdata/bla bla/newdata/bla

Try my earlier suggestion, but with | as a separator and use a character set as context:

sed 's|^| |;s|$| |;s|\([^[:alnum:]]\)data\([^[:alnum:]]\)|\1newdata\2|g;s|^ ||;s| $||' infile

This is the general case that should work under most circumstances.

In this particular case you could also use something simpler:

sed 's|/data/|/newdata/|g' infile
s|^| |;s|$| |

could be shorten

s|.*| & |

Try this ,

while read line
do
        if [[ $line ==  *\/data\/* ]]
        then
                echo `sed -r "s/\/data\//\/newdata\//g" <<<$line`
        elif [[ $line ==  *\/newdata\/* ]]
        then
                echo `sed -r "s/\/newdata\//\/data\//g" <<<$line`
        fi
done <"file1"

Ah yes, true :slight_smile: And the reverse?