If you are grep
ping xml
files in this way then your search will return the entire line containing the search string and, if the xml
file has no newlines, the entire file contents. Quite a lot of "other" across 10M files.
As per @Kusalananda comment it isn't good practice to brute force an xml
with grep
and an xml
parser e.g. xmllint
is a better tool, however, if you insist ......
Check man
for grep
and read up on the -o
option to restrict the returned value and use regex
that defines the whole length of the match you are looking to find.
If username
is an attribute
grep -o 'username="[^"]*"'
Or better
xmllint --xpath "//@username"
If username
is a node then something like
grep -o "username>[^<][^<]*"
Or better
xmllint --xpath "//username"
With either of the xmllint
queries, just wrap the query in string()
to extract the attribute or node text.
xmllint --xpath "string(//username)"
xmllint --xpath "string(//@username)"
grep
. – Kusalananda Nov 08 '19 at 12:10