1

I have an XML file of format,

 {XML file with similar tags - SubRecord and Property}
 ...

 <SubRecord>
 <Property Name="Name">My Main Search Keyword</Property>
 <Property Name="Prompt">Dummy</Property>
 <Property Name="Default">Value i'm Concerned to Modify</Property>
 </SubRecord>

 ...

My req. is to get the value of the "Default" for this particular sub-record and update it based on condition. For that i need to get to this particular tag "Name" and modify its value.

Is there way using SED/AWK/GREP?

EDIT: As per @terdon's update:

  • Will all sections be one line only? No
  • Will the default always be the last? Always third from top (Name, Prompt, Default)
  • Is anything case sensitive? Is everything? Case Sensitive.
  • Are there blank lines? Hopefully No. But i can do some pre-possessing to remove them.
  • Is the file indented? Yes.

Example: ...

 <SubRecord>
 <Property Name="Name">Search</Property>
 <Property Name="Prompt">Some Text</Property>
 <Property Name="Default">abc.txt</Property>
 </SubRecord>

 ...

In a large XML file with similar Propert and SubRecords, i need to first find all the properties of "Search" parameter.

On finding "Search" i then need to check its default value. If it is abc.txt, then i need to retain that value, if it is xyx, still i need to retain. Other than abc.txt or xyz, i need to update it with abc.txt.

Dud
  • 33
  • 1
  • 1
  • 4
  • based on condition - what condition? – RomanPerekhrest May 02 '17 at 16:46
  • depending on the Value within "Value i'm Concerned to Modify". I think i can handle that myself once i grab hold of it. – Dud May 02 '17 at 16:49
  • if its "Value i'm Concerned to Modify", retain the value, if its "Value i'm not Concerned to Modify", update with "Value i'm Concerned". – Dud May 02 '17 at 16:50
  • First of all, use a dedicated parser. Anything else is very fragile. if you insist on not using a parser, [edit] your question and show us an example of the output you are expecting. Also clarify how variable your file is. Will all <property></property> sections be one line only? Will the default always be the last? Can you have nested tags? Is anything case sensitive? Is everything? Are there blank lines? Is the file indented? if you don't want to use a parser, you will need to be very specific about your requirements. – terdon May 02 '17 at 16:54
  • Will all sections be one line only? No Will the default always be the last? Always third from top (Name, Prompt, Default) Is anything case sensitive? Is everything? Case Sensitive Are there blank lines? Hopefully No. But i can do some pre-possessing to remove them. Is the file indented? Yes. – Dud May 02 '17 at 16:59
  • 1
    Please [edit] your question as I asked in my comment. Comments are easy to miss and hard to read. Just edit your question so that it reads as though you had included the information there from the beginning. The comments can then be deleted. Since you indicate your file can change (multiline stuff etc) make sure to include an example that shows all these possibilities. And don't forget to include your desired output. – terdon May 02 '17 at 17:00

3 Answers3

4

XML should be handled by an XML-aware tool.

XMLStarlet is such a tool.

This is how you set the value of the Property node whose Name attribute is Default and whose value is not abc.txt or xyx:

xml ed -u '//SubRecord/Property[@Name="Default" and . != "xyx" and . != "abc.txt"]' -v 'abc.txt' file.xml

Given an XML file like the following:

<?xml version="1.0"?>
<Record>
  <SubRecord>
    <Property Name="Name">My Main Search Keyword</Property>
    <Property Name="Prompt">Dummy</Property>
    <Property Name="Default">zzz</Property>
  </SubRecord>
  <SubRecord>
    <Property Name="Name">My Main Search Keyword</Property>
    <Property Name="Prompt">Dummy</Property>
    <Property Name="Default">abc.txt</Property>
  </SubRecord>
  <SubRecord>
    <Property Name="Name">My Main Search Keyword</Property>
    <Property Name="Prompt">Dummy</Property>
    <Property Name="Default">xyx</Property>
  </SubRecord>
</Record>

this produces

<?xml version="1.0"?>
<Record>
  <SubRecord>
    <Property Name="Name">My Main Search Keyword</Property>
    <Property Name="Prompt">Dummy</Property>
    <Property Name="Default">abc.txt</Property>
  </SubRecord>
  <SubRecord>
    <Property Name="Name">My Main Search Keyword</Property>
    <Property Name="Prompt">Dummy</Property>
    <Property Name="Default">abc.txt</Property>
  </SubRecord>
  <SubRecord>
    <Property Name="Name">My Main Search Keyword</Property>
    <Property Name="Prompt">Dummy</Property>
    <Property Name="Default">xyx</Property>
  </SubRecord>
</Record>

(the first SubRecord has been modified)

XMLStarlet is available from http://xmlstar.sourceforge.net/ (but check your own package manager first). Sometimes its executable is called xmlstarlet rather than just xml.

Kusalananda
  • 333,661
3

You can't parse [X]HTML/XML with regex. Because HTML/XML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML/XML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML/XML. HTML/XML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML/XML into its meaningful parts. Even enhanced irregular regular expressions as used by Perl are not up to the task of parsing HTML/XML. You will never make me crack. HTML/XML are languages of sufficient complexity that it cannot be parsed by regular expressions. Even Jon Skeet cannot parse HTML/XML using regular expressions. Every time you attempt to parse HTML/XML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp. Parsing them with regex summons tainted souls into the realm of the living. They and regex go together like love, marriage, and ritual infanticide. The <center> cannot hold it is too late. The force of regex and HTML/XML together in the same conceptual space will destroy your mind like so much watery putty. If you parsewith regex you are giving in to Them and their blasphemous ways which doom us all to inhuman toil for the One whose Name cannot be expressed in the Basic Multilingual Plane, he comes. HTML-plus-regexp will liquify the n​erves of the sentient whilst you observe, your psyche withering in the onslaught of horror. Rege̿̔̉x-based HTML/XML parsers are the cancer that is killing StackOverflow it is too late it is too late we cannot be saved the trangession of a chi͡ld ensures regex will consume all living tissue (except for HTML which it cannot, as previously prophesied) dear lord help us how can anyone survive this scourge using regex to parse HTML has doomed humanity to an eternity of dread torture and security holes using regex as a tool to process HTML establishes a breach between this world and the dread realm of c͒ͪo͛ͫrrupt entities (like SGML entities, but more corrupt) a mere glimpse of the world of reg​ex parsers for HTML will ins​tantly transport a programmer's consciousness into a world of ceaseless screaming, he comes, the pestilent slithy regex-infection wil​l devour your HT​ML parser, application and existence for all time like Visual Basic only worse he comes he comes do not fi​ght he com̡e̶s, ̕h̵i​s un̨ho͞ly radiańcé destro҉ying all enli̍̈́̂̈́ghtenment, HTML tags lea͠ki̧n͘g fr̶ǫm ̡yo​͟ur eye͢s̸ ̛l̕ik͏e liq​uid pain, the song of re̸gular exp​ression parsing will exti​nguish the voices of mor​tal man from the sp​here I can see it can you see ̲͚̖͔̙î̩́t̲͎̩̱͔́̋̀ it is beautiful t​he final snuffing of the lie​s of Man ALL IS LOŚ͖̩͇̗̪̏̈́T ALL I​S LOST the pon̷y he comes he c̶̮omes he comes the ich​or permeates all MY FACE MY FACE ᵒh god no NO NOO̼O​O NΘ stop the an​*̶͑̾̾​̅ͫ͏̙̤g͇̫͛͆̾ͫ̑͆l͖͉̗̩̳̟̍ͫͥͨe̠̅s ͎a̧͈͖r̽̾̈́͒͑e n​ot rè̑ͧ̌aͨl̘̝̙̃ͤ͂̾̆ ZA̡͊͠͝LGΌ ISͮ̂҉̯͈͕̹̘̱ TO͇̹̺ͅƝ̴ȳ̳ TH̘Ë͖́̉ ͠P̯͍̭O̚​N̐Y̡ H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝

DopeGhoti
  • 76,081
  • (hat tip to http://stackoverflow.com/users/18936) – DopeGhoti May 02 '17 at 17:13
  • I have been looking in the relative stackoverflow post again and again. Could you kindly clarify if the 'it's all greek to me' chars in the end of this post are there ON PURPOSE? – George Vasiliou May 02 '17 at 22:35
  • I can clarify that the descent into madness is deliberate. – DopeGhoti May 02 '17 at 22:42
  • 1
    It's to make it perfectly clear that there's a bunch of meta characters that really screw with XML when regular-expressioning it. And thus you'd be better off using a parser. – Sobrique May 11 '17 at 09:15
0

This the sed equivalent:

$ cat file9
 <SubRecord1>
 <Property Name=Name>My Main Search Keyword</Property>
 <Property Name=Prompt>Dummy</Property>
 <Property Name=Default>Value i'm Concerned to Modify</Property>
 </SubRecord1>
 <SubRecord2>
 <Property Name=Name>My Main Search Keyword</Property>
 <Property Name=Prompt>Dummy</Property>
 <Property Name=Default>Do not Modify</Property>
 </SubRecord2>

$ sed -r '/\bSubRecord1\b/!b;n;n;n;s/(<Property Name=Default>)(.*)(<\/Property>)/\1AAAA\3/' file9
 <SubRecord1>
 <Property Name=Name>My Main Search Keyword</Property>
 <Property Name=Prompt>Dummy</Property>
 <Property Name=Default>AAAA</Property>
 </SubRecord1>
 <SubRecord2>
 <Property Name=Name>My Main Search Keyword</Property>
 <Property Name=Prompt>Dummy</Property>
 <Property Name=Default>Do not Modify</Property>
 </SubRecord2>

The use of word boundaries \b ensures that sed pattern /SubRecord/ will not match SubRecord1 or SubRecord2