Papoy Edits Papoy Edits - 3 years ago 292
Bash Question

Use grep to extract 6 arbitrary digits not starting with 3 binary digits

I have received an assignment that i'm struggling with where, somewhat simplified, i need to extract 6 arbitrary digits from a dataset that cannot start with 3 binary digits. I am only allowed to use grep-commands. For example, from this dataset:


i want to extract these pieces:


So it is allowed for multiple matches on the same row, and it can start on less than 3 binary digits, but not exactly 3 binary digits. I've fiddled a bit with various regex concepts but so far i only have this base code, because my inexperienced mind can't see any possible approach to separate the binary digits:

grep -o '[0-9]\{6\}'

Answer Source

Make it in two steps

grep -Po -e '([01]{3})*(\d{6})' | grep -Po '(\d{6})$'

First getting the matching digits with/without leading 'binary digits'. Second, the 6 digits at the end.

The same thing without extended RegEx

grep -Eo '([01]{3})?([0-9]{6})' | grep -Eo '([0-9]{6})$'
grep -o '\([01][01][01]\)*\([0-9][0-9][0-9][0-9][0-9][0-9]\)' | grep -o '\([0-9][0-9][0-9][0-9][0-9][0-9]\)$'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download