0

How do i search a particular line based on string match and replace it with another sting. Below is the example of a html strong which i need to modify using bash script.

< link type="text/css" rel="stylesheet" href="https://teststore.xxx.com/store/downpanel.css">

change to:

< link type="text/css" rel="stylesheet" href="https://testsstore.xxx.com/store/downpanel.css">

i.e teststore with testsstore. just trying to add 's' .

I guess i need to match all the string. because downpanel.css is the one which differentiate which line to be edit with 's'.

I being said that this can be achieved by Regualar expression.. but i couldn't able to make it . any help with syntax would be highly greatful.

thanks.
jack

Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
Johnbritto
  • 149
  • 2
  • 14
  • 2
    Ob http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 : don't use regex to match HTML. – geekosaur Apr 09 '12 at 10:22

2 Answers2

1

If you need to replace all occurrences of this link, just do

sed 's_"https://teststore.xxx.com/store/downpanel.css"_"https://testsstore.xxx.com/store/downpanel.css"_g' old_file > new_file

If you really need to match the whole part you show, then put in in the sed command. Beware of line breaks, they will spoil the match if encountered somewhere in the middle.

Here's a reference for sed. Or just type man sed on Linux.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • thanks all of you.. ..sed 's_"https://teststore.xxx.com/store/downpanel.css"_"https://testsstore.xxx.com/store/downpanel.css"_g' did worked as i was trying. – Johnbritto Apr 09 '12 at 12:08
  • @Johnbritto: Don't forget to accept the answer that helped you. See http://stackoverflow.com/faq#howtoask – johnsyweb May 14 '12 at 11:55
0

To solve problems like this you generally use a sed construct like this:

sed -e '/unique pattern/s/string or pattern/replacement string/' file

The initial /unique pattern/ part constrains the substitution to lines which the pattern given. For simple cases, like yours, it may be sufficient to simply perform the substitution on all lines containing teststore, thus the unique pattern part can be omitted and the substitute becomes global:

sed -e 's/teststore/testsstore/' file

If this is a problem for you and replaces something it should not you can use the original form, perhaps like this:

sed -e '/link.*teststore/s/teststore/testsstore/' file

To help limit the impact.

Note that this will only write the modified version of file to stdout. To make the change in place, add the -i switch to sed.

sorpigal
  • 25,504
  • 8
  • 57
  • 75