Reputation: 21
I am trying to regex this single line user_agent field.
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/437.38 (KHTML, like Gecko) Chrome/49.0.3477.100 Safari/437.38"
cat myfile | grep -oP '(user_agent=[^ ]*)' | awk {'print $1'}
The command above returns
“user_agent="Mozilla/5.0 “
only. However I need a whole text
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/437.38 (KHTML, like Gecko) Chrome/49.0.3477.100 Safari/437.38"
to be matched.
Please help modify the regex pattern I used.
Upvotes: 0
Views: 126
Reputation: 333
The problem you are facing is 2 fold.
So you need to let go of the awk print and use .*
in place of *
with a positive lookahead.
Check the following:
cat myfile | grep -oP '(user_agent=\".*\")(?=\saccept)'
Here,
\".*\"
is searching for everything that is within double quotes
(?=\saccept)
is a positive lookahead statement which stops your search as soon as it finds a space followed by accept
.
Upvotes: 1