I need to modify an existing script which uses SED to search and replace in a file.
My problem is that i need to use sed to do the same search and replace but only if 2 criteria is met.
I use find and sed like below :
find . -name 'search.do*' -exec sed -i "/href/\
s/href=\"[^\"]*sommerhus\/detail\.do[^\"]*unitId=\([^\"]*\)\"/href=\"sommerhus-\1.html\"/g" {} \;
but now i need to make sure that the replacement only takes place if the “href” AND another search criteria is met f.x “href” && “language” is present
I have tried with -e but without success.
Is it possible ??
You could use
/href.*language\|language.*href/(possibly with some extra constraints, like a space before the second word in each match).You could also nest the commands in blocks:
This is more extensible: adding a third pattern only requires adding in one pair of braces and the pattern, while the alternation solution above requires 6 patterns in total (one for each possible order).
Also, to save having to backslash the
/inside the pattern, sed allows you to use any character as a separator, e.g.@:(A small gain in this case, but nice to know for other situations when handling file paths or URLs etc.)