Search replace in XML file with sed or awk

Bob Lyman

So I have a task where by I have to manipulate an XML file through a bash shell script.

Here are the steps:

  1. Query XML file for a value.
  2. Take the value and cross reference it to find a new value from a list.
  3. Replace the value of a different element with the new value.

Here is a sample of the XML with non-essential info removed:

<fmreq:fileManagementRequestDetail xmlns:fmreq="http://foobar.com/filemanagement">
      <fmreq:property>
         <fmreq:name>form_category_cd</fmreq:name>
         <fmreq:value>Memos</fmreq:value>
      </fmreq:property>
      <fmreq:property>
         <fmreq:name>object_name</fmreq:name>
         <fmreq:value>Correspondence</fmreq:value>
      </fmreq:property>
</fmreq:fileManagementRequestDetail>

I have to get the value from the value element under object_name, cross reference it, and then replace the value under the form_category_cd value element with the new value:

So if object_name -> value is Correspondence then the form_category_cd -> value might need to be YYZ.

Here's the rub, I can only use the tools available on our server as our operations group is restricting us to the tools at hand. It was a fight to get xmllint updated and then it got overruled. I'm on a version that does not support --xpath, which believe me is difficult on a good day. Also the version I have available doesn't support namespaces, so xmllint is out.

I've tried sed, but it seems to not like my regex even though every tester I try works fine.

Regex:

(<fmreq\:name>object_name<\/fmreq\:name>)(?:\n\s*)(<fmreq\:value>)(.*)(<\/fmreq\:value>)

I need to get group #3, but sed won't return it. Instead it returns the entire contents of the XML file.

sed -e 's/\(<fmreq\:name>object_name<\/fmreq\:name>\)\(?:\n\s*\)\(<fmreq\:value>\)\(.*\)\(<\/fmreq\:value>\)/\3/' < c3.xml 

I'm not as familiar with awk / gawk, so I'm struggling to figure them out and this as well, but am open to them if a solution can be found.

Would love to have an awk / gawk solution just to make the boss happy since he's an old awk fan, but I'll take what I can get as I'm stumped.

Again I have to use the tools on hand and can't install anything new.

Rastapopoulos

I think that there are a couple of problems in your sed command:

  • You don't use the -n option, so by default sed just prints every line of input to the output (possibly modified by a sed command).

  • You don't need the redirection < c3.xml, because sed recognizes the last argument as a filename.

  • sed is not very well suited for matches over multiple lines. See for example here.

The following seems to work on your example:

sed -n "/<fmreq:name>object_name<\/fmreq:name>/ {n;p}" c3.xml | sed "s/^\s*<fmreq:value>\(.*\)<\/fmreq:value>/\1/g"

Or, with only one sed invocation:

sed -n "/<fmreq:name>object_name<\/fmreq\:name>/ {n;s/^\s*<fmreq:value>\(.*\)<\/fmreq:value>/\1/g;p}" c3.xml

Breakdown of what this command does:

  • The option -n tells sed not to print the pattern space after it's finished processing the line. Consequently, you need to use the command p explicitely to do so.

  • /regex/ tells sed to execute the commands that follow only on the lines that match regex.

  • The sed command n replaces the content of the pattern space by the next line of input, which is the one containing the value you are interested in.

  • The sed command s/regex/replacement/ substitutes the first match of regex in the pattern space by replacement.

  • The sed command p prints the line.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Insert File Path Variable into Sed Search/Replace

分類Dev

How to replace a xml file line using sed

分類Dev

Find and replace a character in xml file using sed command is not working

分類Dev

Replace text in file with SED

分類Dev

sed - Search and Replace Multiple Line

分類Dev

Inline search and replace using sed

分類Dev

Sed replace specific line in file

分類Dev

Sed replace specific line in file

分類Dev

sed command - Replace string in file

分類Dev

hex search and replace characters with sed linux

分類Dev

Bash while loop search and replace using sed

分類Dev

Search and replace only part of the text using awk

分類Dev

Search pattern from tag and inserting it to next line by using sed or awk

分類Dev

How to replace values in a file using sed

分類Dev

using Sed to find and replace a string inside a file

分類Dev

Search and replace specific code in xml using perl

分類Dev

Read file, search text and replace all the line

分類Dev

sed replace 1 line in file with all lines in file

分類Dev

add quotes to words matching regex in file awk or sed

分類Dev

deleting lines between rows in a text file using awk or sed

分類Dev

How to replace an email in a (PHP) file from the command line without sed?

分類Dev

How to do replace using sed only in one section of file

分類Dev

How to use variable in sed command to replace a property value in a property file

分類Dev

Replace a string in the last 100 records on a file using awk

分類Dev

Multiple search and replace actions in one large text file

分類Dev

Search for specific line of text file, replace up to a certain character

分類Dev

How to search and replace dual characters by Unicode single characters in a garbled file?

分類Dev

Sed / awk正規表現:XMLフィード

分類Dev

what can I use sed to search for a string and append /sub1 and some file ../ before search string

Related 関連記事

  1. 1

    Insert File Path Variable into Sed Search/Replace

  2. 2

    How to replace a xml file line using sed

  3. 3

    Find and replace a character in xml file using sed command is not working

  4. 4

    Replace text in file with SED

  5. 5

    sed - Search and Replace Multiple Line

  6. 6

    Inline search and replace using sed

  7. 7

    Sed replace specific line in file

  8. 8

    Sed replace specific line in file

  9. 9

    sed command - Replace string in file

  10. 10

    hex search and replace characters with sed linux

  11. 11

    Bash while loop search and replace using sed

  12. 12

    Search and replace only part of the text using awk

  13. 13

    Search pattern from tag and inserting it to next line by using sed or awk

  14. 14

    How to replace values in a file using sed

  15. 15

    using Sed to find and replace a string inside a file

  16. 16

    Search and replace specific code in xml using perl

  17. 17

    Read file, search text and replace all the line

  18. 18

    sed replace 1 line in file with all lines in file

  19. 19

    add quotes to words matching regex in file awk or sed

  20. 20

    deleting lines between rows in a text file using awk or sed

  21. 21

    How to replace an email in a (PHP) file from the command line without sed?

  22. 22

    How to do replace using sed only in one section of file

  23. 23

    How to use variable in sed command to replace a property value in a property file

  24. 24

    Replace a string in the last 100 records on a file using awk

  25. 25

    Multiple search and replace actions in one large text file

  26. 26

    Search for specific line of text file, replace up to a certain character

  27. 27

    How to search and replace dual characters by Unicode single characters in a garbled file?

  28. 28

    Sed / awk正規表現:XMLフィード

  29. 29

    what can I use sed to search for a string and append /sub1 and some file ../ before search string

ホットタグ

アーカイブ