An example, test.txt:
This is bad, real bad!
<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions targetNamespace="http://tips.cf"
xmlns:impl="http://tips.cf" xmlns:intf="http://tips.cf"
xmlns:apachesoap="http://xml.apache.org/xml-soap"
I have a regex: ^<\?xml.*\?>.
grep match as line by line. So this regex can have a match(the second line).
But I want to make grep treat the lines as a big line, and couldn't have a match, because it is not startswith <?xml.
I tried:
grep -P -z -- '^<\?xml.*\?>' test.txt
use -z but it still match the second line.
Is there a way to use grep to make it unmatch, or there is another regex command line tool?
If you use \A instead of anchor ^ then it will fail the match:
# finds no match
grep -Pz -- '\A<\?xml.*\?>' file
This grep in a multiline string ^ matches at the start of every line but \A matches at the real start of input.
grep pattern containing newline (bash: $'\n')Try this:
grep -Pz '\AThis.*\n<\?xml.*\?>' test.txt
, this
grep -Pz '<\?xml.*\?>' test.txt
, this
grep -Pz '^<\?xml.*\?>' test.txt
and this
grep -Pz '\A<\?xml.*\?>' test.txt
or this
grep -z $'^This.*\n<\\?xml.*\\?>' test.txt
You can join the lines to a big line by xargs before applying the regex:
# no match returns
cat test.txt | xargs | grep '^<?xml.*?>'