0

I have multiple XML files with strings that are all set to

<msg type="Status"

Some of these strings should be type "Warning" and I have a separate text file with the warning strings.

I can do a

grep -f strings.txt *.xml

this allows me to see which warning strings are incorrectly listed as status strings. What I would like to do is

grep -f strings.txt *.xml | sed 's/status/warning/'

This gives me my desired output, but it is only being displayed (not saved). There are multiple xml files so i can't just save the output to one file. I need sed to replace the string in the original .xml file it originated from. Thanks for your help.

0x90
  • 39,472
  • 36
  • 165
  • 245
lacosteaef
  • 55
  • 5

3 Answers3

2

you were close instead of grep -f strings.txt *.xml | sed 's/status/warning/'

do

grep -l strings.txt *.xml | xargs sed -i 's/status/warning/g'

you forgot to use xargs, read here for more info about xargs.

0x90
  • 39,472
  • 36
  • 165
  • 245
  • only using -rl doesn't produce anything, sorry for all the questions – lacosteaef Dec 09 '13 at 19:53
  • run this command: `grep -l strings.txt *.xml | xargs sed -i 's/status/warning/g'` I fixed it. – 0x90 Dec 09 '13 at 19:59
  • If it do `grep -f strings.txt *.xml` i see all my shared lines. If i `grep -l strings.txt *.xml` I don't get any output. Thanks for all the troubleshooting help. – lacosteaef Dec 09 '13 at 20:04
  • Specifically I see something like this `vnlrmsg.xml:` – lacosteaef Dec 09 '13 at 20:05
  • so do `grep -fl strings.txt *.xml |xargs sed -i 's/status/warning/g';` otherwise I don't know what you are using. – 0x90 Dec 09 '13 at 20:06
  • 1
    In the end it worked. I'm using cygwin on this box and I had to use `dos2unix` on my files for everything to work correctly. Thanks. – lacosteaef Dec 12 '13 at 18:55
0

Recommend trying xmllint. Using the --pattern option, you can specify xpath search queries to search for exactly what you need it to find without complicated regular expressions.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
0

There is an Perl script xml_grep using xpath as query language: http://search.cpan.org/dist/XML-Twig/tools/xml_grep/xml_grep

xml_grep '//msg[@type="Warning"]' *.xml
alex
  • 1,304
  • 12
  • 15