Script to search a string which is in between "" and replace it with another character

Hi,

I am trying to search a string from a text file which is in between "" (Double Quotes) (Eg: "Unix"), and replace it with a | where ever it is appearing in the text file and save the file.

Please help me.

-kkmdv

Can you show us the code you have so far, and tell us how it misbehaves?

Hi Franklin,

here is my code which I tried,

#!/bin/bash
sed '/"/,/"/ !d' sample.txt > sample.txt

but it is deleting the entire data from the file.

I don't believe you can direct the output back into your input file. Direct it to something other than sample.txt.

To replace the word Unix between double quotes this should be sufficient:

sed 's/Unix/|/g' file > tempfile
mv tempfile file

If your sed version supports the -i option:

sed -i 's/Unix/|/g' file

To replace every word between double quotes you could use:

awk -F"\"" '{for(i=2;i<NF;i+=2){$i="|"}}1' OFS="\""  file > tempfile
mv tempfile file

Franklin,

THanks for your promt response. your script is working fine but, my problem is,

the script needs to search something like in between double quotes and replace it with | . doesnt matter what ever there in between double quotes.

Eg: if the text file contains something like "<Some text>",after executing the script, it should show like "|".

Thanks,
-kkmdv

The awk command should work in that case. Have you tried it?