Eggfoot
Eggfoot

Reputation: 49

Removing part of a random string using sed or grep (Or any other regex solution)

I have an XML File generated by a CMS that denotes what UI buttons should be displayed for a touchtable application. Please find below:

</GlobalMenu>
 <Buttons>
  <Button name="salford_121"/>
  <Button name="salford_197" title="SkillsWorkshop"/>
  <Button name="salford_121"/>
  <Button name="salford_190" title="Salford Students"/>
  <Button name="salford_121"/>
  <Button name="GlobalMenu.xml" title="Main Menu"/>
 </Buttons>
</GlobalMenu>`

When this file is generated I need to run a bash script that removes title="xxxxx" leaving the /> at the end of each line. The title could have any sequence of alpha numeric characters and symbols up to 80 characters long including spaces. I also need it to ignore lines with title="Main Menu" so not to delete the go back function of the menu that returns to the GlobalMenu.xml file. It would also be backed up to a custom.bak file.

Im trawling through any Regex tutorials, but Im finding it difficult to write something using sed or grep for changing RANDOM substrings, as these titles could be anything. Please forgive my noobness Ive only been at this for 5 days.

Upvotes: 0

Views: 349

Answers (2)

anubhava
anubhava

Reputation: 785376

You can use this sed:

sed -i.bak '/ title="Main Menu"/!s/ title="[^"]*"//' file.xml

cat file.xml
</GlobalMenu>
 <Buttons>
  <Button name="salford_121"/>
  <Button name="salford_197"/>
  <Button name="salford_121"/>
  <Button name="salford_190"/>
  <Button name="salford_121"/>
  <Button name="GlobalMenu.xml" title="Main Menu"/>
 </Buttons>
</GlobalMenu>

Upvotes: 4

You're looking for the . special character, which matches anything. However you want something more like [a-zA-Z ] to match a string containing only the alphabet and spaces (. is greedy). If you want to include numbers as well, you can specify \d: [a-zA-Z \d]

/title="[a-zA-Z \d]"/g

I also tend to use regexpal.com to double-check my search strings.

Upvotes: -1

Related Questions