New answers tagged


No one has said it yet, so I will. PLEASE don't parse XML using regular expressions. XML is a contextual language, and regular expressions aren't. This means you create brittle code, that one day might just break messily. For more examples, see: PLEASE use a ...


Given nonancient GNU awk (gawk) and assuming each pair of testname and enabled are on the same line separate from any other pair (which is not a given for XML in general): awk 'match($0,/ testname="([^"]+)"/,a) {sub(/ enabled="[^"]+"/, " enabled=\"" (a[1]~/AA/?"true":"false") "\"")} 1' <input Explanation: match($0,/ testname="([^"]+)"/,a) returns ...


If I understood the logic correctly, this sed command searches for the given $1 parameter inside of the testname value; if it's present, then search and replace the enabled value from false to true. If it's not (!) present, then replace the enabled value from true to false. sed '/ testname="[^"]*'$1'[^"]*"/ s/ enabled="false"/ enabled="true"/; / ...


Sorry, try this: cat xml | sed 's/\(testname\=\".*AA.*\"\s\)enabled="\(true\|false\)"/\1enabled=\"true\"/gi'

Top 50 recent answers are included