1

When I query docker images, I get this:

#> docker images
REPOSITORY          TAG            IMAGE ID           CREATED             VIRTUAL SIZE
local/debian        7.5            172c7702a46f       2 weeks ago         175.1 MB
local/debian        wheezy         172c7702a46f       2 weeks ago         175.1 MB

Using grep I extract the image tags:

#> docker images | grep -oP '\S+(?=\s+172c7702a46f)'
7.5
wheezy

How can I refine this to get wheezy only and avoid anything that matches \d+\.\d+?

dawud
  • 15,096
  • 3
  • 42
  • 61
balin
  • 123
  • 1
  • 5
  • You can easily solve this by reading the `grep` manpage. As an alternative, you can also use `awk`: `docker images | awk '$2!~/[[:digit:]]/{print $2}'`. – dawud May 30 '14 at 20:01
  • You are right ... I end up with `docker images | grep -oP '\S+(?=\s+172c7702a46f)' | grep -vP '\d+\.\d+'`. Took me an eternity to understand why `-o` in the second `grep` did not yield anything ... woudl have preferred forgoingn the second `grep` though. Can all of that be packed into a single regexp? – balin May 30 '14 at 20:11
  • Add `[a-z]` before `\S+` to match `y` from wheezy: `grep -oP '[a-z]\S+(?=\s+172c7702a46f)'`. – Cyrus Jul 27 '14 at 12:47

1 Answers1

1

The grep expression is too complicated. Use awk for a more straightforward, clear solution that doesn't required a regex wizard to read:

docker images | awk '$3 == "172c7702a46f" && $2 ~ /^[a-z]/ {print $2}'

That is: if column 3 is 172c7702a46f, and column 2 starts with letters, print column 2.

janos
  • 808
  • 1
  • 6
  • 22
  • Look ahead / look behind are common regex notions. That's not *too complicated*, the only thing is : people are not used to regex flavours in a general way and call everything complicated once it goes further than `[a-z0-9]` stuff. It's also more concise and faster at runtime to write `grep -oP '[[:alpha:]]+(?=[[:space:]]+172c7702a46f)'` than a bunch of awk conditions. – Xavier Lucas Dec 28 '14 at 17:40