Shell script to parse a line and insert a word

Hi All,
I have a file like this,
data1,data2,[data3],[data4],data5,data6.

i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this .
data1,data2,[/example/string],[data4],data5,data6.

Could you guys help me to get a sed command or any other command to perform my above task.
I tried used a sed command like "echo "s/\[/\example\/string" /g > temp
sed -f temp2 data_file> tst_one.
cat tst_one
But this gives me the output as below,
data1,data2,[/example/string],[/example/string],data5,data6.

Thanks in Advance!,
Giri.

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

Hi zaxxon,
My apologies for not following the forum standards, since i am very new to this forum i was not aware of this stuffs. I shall keep all the points mentioned by u when posting my new query.

Thanks,
Giri.

What you did is replacing "[" (and not "data3") with "/example/string"
Regards.

No i want to replace data3 and not '['.

Thanks,
Girish

Try this,

sed -e "s/\[data3\]/[\/example\/string\]/g"

We understood that - but this is what you actually did with your regexp:

If you want to replace "data3" with "something_else" the regexp for this is:

s/data3/something_else/

It might be that "data3" is occurring several times in the text and you want to change only the occurrences where "data3" is enclosed in rectangular brackets (change "[data3]", but not, say, "(data3)"), preserving the brackets.

for this the regexp would have to look like:

s/[data3]/[something_else]/

Notice that i put the the brackets to be preserved in the replacement string. Regexps work like that: first the search-part is searched for and - if it is found at all - everything matching this first part is removed from the line. Then the replacement string is prepared and inserted into the place where the search-string was.

As we matched to brackets before they would have been removed if they are not being reinserted with the replacement.

I hope this helps.

bakunin

Thanks a lot to everyone for the quick reply and help.. now my script works

echo a[bb]c | sed 's/\[[^]]*\]/\[newdata\]/'