0

When I run my command I get:

sed 's/<?xml version="1.0" encoding="UTF-8"?>//2g' all.xml
sed: 1: "s/<?xml version="1.0" e ...": more than one number or 'g' in substitute flags

How can I fix this? It doesn't make much sense to me. I'm trying to remove all but the first instance of <?xml version="1.0" encoding="UTF-8"?> from a new xml file.

user3440639
  • 185
  • 2
  • 12
  • What does your input data look like? – that other guy Sep 17 '19 at 22:06
  • @thatotherguy imagine the same XML file combined several times. So there are multiple instances of ` ` – user3440639 Sep 17 '19 at 22:08
  • Your snippet would require all these instances to be on the same line. Is that what you have? – that other guy Sep 17 '19 at 22:10
  • @thatotherguy no, separate lines. – user3440639 Sep 17 '19 at 22:10
  • Which OS version and sed version do you use? – Cyrus Sep 17 '19 at 22:16
  • @Cyrus macOS mojave – user3440639 Sep 17 '19 at 22:19
  • 3
    You'll find a lot more people willing to read input you provide than imagine input you describe. [edit] your question to show concise, testable sample input and expected output. See [ask]. – Ed Morton Sep 17 '19 at 22:54
  • Some call it [summoning the daemon](https://www.metafilter.com/86689/), others refer to it as [the Call for Cthulhu](https://blog.codinghorror.com/parsing-html-the-cthulhu-way/) and few just [turned mad and met the Pony](https://stackoverflow.com/a/1732454/8344060). In short, never parse XML or HTML with a regex! Did you try an XML parser such as `xmlstarlet`, `xmllint` or `xsltproc`? – kvantour Sep 18 '19 at 09:17

1 Answers1

0

You sed line does not give error on my Ubuntu 18.04, but it does not work. sed is very good to work with one line at a time, but harder to get to work across multiple lines.

awk may be a better solution.

awk '/<\?xml version="1\.0" encoding="UTF-8"\?>/ && f++>0 {next} 1' file

This will test your line if it contains the patten and test if flag f is larger than 0 and then increase flag f by 1. So in first hit, the flag is 0 and line is printed. For all other find flag f us larger than one and the next makes us skip the line and go to next. The last 1 is always true, so if its run, do the default action, print the line.

Jotne
  • 40,548
  • 12
  • 51
  • 55