sed/Awk/cut... How to decide which to use to parse Docker output?
Your input seems to have a fixed width of 20 chars for each field, so you can make use of gawk's FIELDWIDTHS
feature.
$ awk -v FIELDWIDTHS="20 20 20 20 20" '{ print $3 }' fileIMAGE ID806f56c844440da05d84b1fe$$ awk -v FIELDWIDTHS="20 20 20 20 20" '{ printf "%20s%20s\n", $1, $3 }' fileREPOSITORY IMAGE IDjenkins/jenkins 806f56c84444mongo 0da05d84b1fe
From man gawk
:
If the FIELDWIDTHS variable is set to a space-separated list of numbers, each field is expected to have fixed width, and gawk splits up the record using the specified widths. Each field width may optionally be preceded by a colon-separated value specifying the number of characters to skip before the field starts. The value of FS is ignored. Assigning a new value to FS or FPAT overrides the use of FIELDWIDTHS.
You have to "squeeze" the space padding in the default output to single space.
1 2
== 1-space-space-2
== Field 1 before 1st space, Field between 1st and 2nd space, Field 3 after 2nd space.
cut -d' ' -f1
==> '1'
cut -d' ' -f2
==> '' empty field between 1st and 2nd delimiter
cut -d' ' -f3
==> '2'
So, in your case use sed
to replace consecutive spaces with 1:
docker images | sed 's/ */ /g' | cut -d " " -f1,3
If the output is fixed columns widths, then you can use this variant of cut:
docker images | cut -c1-20,41-60
This will cut out columns 41 to 60, where we find the Image ID.
If ever the output uses TAB
for padding, you should use expand -t n
to make the output consistently space padded then apply the appropriate cut -cx,y
, e.g. (numbers may need adjusting):
docker images | expand -t 4 | cut -c1-20,41-60