I want to substitute text in-between only specific html tag, example html file
original text
original text
original text
original text
Here I want to replace text "original" by "modified" only in-between the tag "title" I tried the following but that replaces everywhere except before title tag. I do need to keep global "g".
sed -i '' '//,/<\/title>/ s/original/modified/g' test.html
Also, I don't understand why do I need ''(two single quote) in my script, for example this one works well (with double quotes)
sed -i -e "s/original/modified/g" test.html
if I have match pattern then the below one doesn't work
sed -i -e "/first/,/last/ s/original/modified/g" test.html
and I need to have '' to get it work as below
sed -i '' '/first/,/last/ s/original/modified/' test.html
I am using Mac OSX 10.9, also not sure of a better way to do this. thanks
Answer
First '' is due to option -i
-i[SUFFIX]'
--in-place[=SUFFIX]'
This option specifies that files are to be edited in-place. GNU
`sed' does this by creating a temporary file and sending output to
this file rather than to the standard output.(1).
sed work line by line by default and pattern address (/start/,/end/) define the line to start until which OTHER line containing the end parttern so in your case where is on the same line, sed start at and stop at the end of file, not like expected
assuming the are always on the same line in your case
sed -i '' '// {
: loop
s|\(.*\)original\(.* \)|\1modified\2|
t loop
}' test.html
No comments:
Post a Comment