Issue
Given a line of the following structure ID: '560'",,10.117.219.156,Browser,fa7a0434-8f0a-47f2-a3ea-3864d652a042,NAME:
I wish to extract the word or words following the IP address (any IP) and ',' upto the following ','. In this example the returned word would be "Browser".
Here is the best I've come up with thus far: (?<=,)(\d+)\.(\d+)\.(\d+)\.(\d+),\w+(?=,)
The expression above does a positive lookbehind for a 'comma', followed by capturing groups resembling an IP address "(\d+).(\d+).(\d+).(\d+)", followed by a 'comma', then the word(s) of interest, followed by a 'comma'.
The problem is that the regex returns the IP address, which i don't want. Here is what i'm getting back when i run: grep -Po '(?<=,)(\d+)\.(\d+)\.(\d+)\.(\d+),\w+(?=,)' /c/tmp/BB1.csv
10.117.245.154,Browser
10.123.33.66,PyCharm IDE
10.117.245.123,Calculator HP
What I want returned is: Browser PyCharm IDE Calculator HP
Solution
You can use
grep -Po ',\d+(?:\.\d+){3},\K[^,]+' /c/tmp/BB1.csv
See the regex demo. Details:
,
- a comma\d+
- one or more digits(?:\.\d+){3}
- three occurrences of.
and then one or more digits,
- a comma\K
- match reset operator that discards all text matched so far from the overall match memory buffer[^,]+
- one or more chars other than a comma.
Answered By - Wiktor Stribiżew Answer Checked By - Willingham (WPSolving Volunteer)