You added the ^
to your input instead of adding it to the regexp in your code that's supposed to match the input, i.e. you did:
$ echo '^foobar' | awk '/bar/'
^foobar
Instead of:
$ echo 'foobar' | awk '/^bar/'
$
You're also using a ?
regexp metachar but want a literal ?
instead and you're trying to use a non-existent keyword quit
when I assume you mean exit
(so what your code actually does is concatenate an undefined variable with the number 0
resulting in the string 0
which you then just discard) but you only exit with 0
which is the default anyway so that's all redundant.
I think this might be what you're trying to do:
awk '/^[[:space:]]*<\?xml /{ f=1; exit } END{ if (f) print "match"; exit !f }'
e.g.:
$ printf '%s\n' '<?xml version="1.1" encoding="UTF-8" standalone="no"?>' '<databaseChangeLog' |
awk '/^[[:space:]]*<\?xml /{ f=1; exit } END{ if (f) print "match"; exit !f }'
match
$ echo $?
0
$ printf '%s\n' 'foo<?xml version="1.1" encoding="UTF-8" standalone="no"?>' '<databaseChangeLog' |
awk '/^[[:space:]]*<\?xml /{ f=1; exit } END{ if (f) print "match"; exit !f }'
$ echo $?
1
The above will work in any POSIX awk. If you have a very old awk that doesn't support POSIX character classes then just change [[:space:]]
to [ \t]
and that will work in any awk.
Consider also printing match or no match to stderr:
$ printf '%s\n' '<?xml version="1.1" encoding="UTF-8" standalone="no"?>' '<databaseChangeLog' |
awk '/^[[:space:]]*<\?xml /{ f=1; exit } END{ print (f ? "" : "no ") "match" | "cat>&2"; exit !f }'
match
$ printf '%s\n' 'foo<?xml version="1.1" encoding="UTF-8" standalone="no"?>' '<databaseChangeLog' |
awk '/^[[:space:]]*<\?xml /{ f=1; exit } END{ print (f ? "" : "no ") "match" | "cat>&2"; exit !f }'
no match