-2

I have the following lines in an XML file

<User id="10338" directoryId="1" sometext txt text test/>
<User id="10359" directoryId="100" some more text text text/>
<User id="103599" directoryId="100" some more text text text/>
<User id="10438" directoryId="1" sometext txt text test/>

I am trying to remove any lines that start with User id=" but I want to keep the ones that have directoryId="1"

my current sed command is

sed -i '' '/<User id="/d' file.xml

I have looked at A regular expression to exclude a word/string and a few other stack overflow posts but not able to get this to work. Please can someone help me write the regex. I essentially need to delete any lines that start with <User id= but excluding the ones where directoryId="1"

Cyrus
  • 77,979
  • 13
  • 71
  • 125
Vik G
  • 357
  • 1
  • 4
  • 19

1 Answers1

0

You can use

sed -i '' -e '/directoryId="1"/b' -e '/<User id="/d' file.xml

With this sed command,

  • /directoryId="1"/b skips the lines containing directoryId="1" and
  • /<User id="/d deletes the other lines that contain <User id=".

See an online demo.

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476