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.

3
  • Any reason you're not using xmllint to parse the file? Commented Dec 9, 2013 at 18:44
  • Probably just ignorance, i'm just working with what I know. Commented Dec 9, 2013 at 18:46
  • It'll save you a lot of grief - you can pass multiple xml files and use the --pattern option to use xpath to search it. Commented Dec 9, 2013 at 18:47

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

only using -rl doesn't produce anything, sorry for all the questions
run this command: grep -l strings.txt *.xml | xargs sed -i 's/status/warning/g' I fixed it.
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.
Specifically I see something like this vnlrmsg.xml:<msg type="Status" text="$$s$$s was previously declared with a different range. (VNLR-1050)" uid="1050"/>
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.
|
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.

1 Comment

Thanks, i'm looking up its usage now. Hopefully it does the trick.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.