1

I work on a small screen and I'm trying to make the output of this command shorter, but I cannot get it to work.

Command: docker container ls --all

Output (too wide!):

CONTAINER ID        IMAGE                                                             COMMAND             CREATED             STATUS                        PORTS               NAMES
e56e7efd4c3c        137112412989.dkr.ecr.us-west-2.amazonaws.com/amazonlinux:latest   "/bin/bash"         32 minutes ago      Exited (127) 31 minutes ago                       ami-test

Output I want:

CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES
e56e7efd4c3c | 137112412989.dkr.ecr.us-west-2.amazonaws.com/amazonlinux:latest | "/bin/bash" | 32 minutes ago | Exited (127) 31 minutes ago | ami-test

Here are some of the 20 or so variations I've tried, none of them have worked:

docker container ls --all  | sed "s/\s+/|/g"

docker container ls --all 2>&1 | sed -e 's/\s+//g'

docker container ls --all 2&1> | sed -e 's/[[:space:]]*/ /'

docker container list --all | sed -e "s/\t/ /g"

I suspect there is something fundamentally wrong with my understanding of pipes and/or sed.

Adam Katz
  • 951
  • 8
  • 17
noctonura
  • 453
  • 1
  • 4
  • 10

3 Answers3

2

Maybe you can also try using the --format option too. Here's the link to the docker reference.

I tried to recreate your desired output using the format option.

root@ubuntu:~# docker container ls --all --format "table {{.ID}} | {{.Image}} | {{.Command}} | {{.CreatedAt}} | {{.Status}} | {{.Ports}} | {{.Names}}"
CONTAINER ID | IMAGE | COMMAND | CREATED AT | STATUS | PORTS | NAMES
5b19f1d7efc1 | ubuntu | "bash" | 2018-10-14 08:11:03 +0000 UTC | Exited (0) 17 minutes ago |  | jovial_spence
05250b252a98 | hello-world | "/hello" | 2018-10-14 08:10:10 +0000 UTC | Exited (0) 18 minutes ago |  | practical_bohr
b206b0ad1f42 | hello-world | "/hello" | 2018-09-27 14:51:49 +0000 UTC | Exited (0) 2 weeks ago |  | nifty_gates
root@ubuntu:~#
rav
  • 54
  • 5
1

The misunderstanding is that be default sed uses basic regular expressions (https://www.gnu.org/software/gnulib/manual/html_node/ed-regular-expression-syntax.html) -- The "one or more" quantifier requires a backslash \+

To use extended regular expressions (https://www.gnu.org/software/gnulib/manual/html_node/egrep-regular-expression-syntax.html) do this:

docker container ls --all  | sed -E 's/\s+/ | /g'

Note that GNU sed accepts \s. Not all seds do.

glenn jackman
  • 4,630
  • 1
  • 17
  • 20
  • 2
    The question has a mac-osx tag, and that comes with bsd `sed` rather than the GNU version, so use `[[:space:]]` instead of `\s`. – Gordon Davisson Oct 14 '18 at 06:53
  • This will give you `32 | minutes | ago` and (as pointed out in the earlier comment) requires GNU `sed` on Mac OS X, which is nonstandard. See [my answer](https://serverfault.com/a/935621/209449) for a portable solution that works around that spacing issue. – Adam Katz Oct 15 '18 at 16:49
1

The best you can do by editing the output with tools like sed is:

docker container ls --all | sed 's/[[:space:]][[:space:]][[:space:]]*/ | /g'

This will work in any (POSIX-compliant) version of sed (no need to worry about GNU support or whether or not to escape a + character) and will require 2+ space characters so you don't get extra delimiters like 32 | minutes | ago

Actually, s/ */ | /g (three literal spaces and then an asterisk) should suffice since I doubt you're getting tabs or more exotic space characters.

Note, the "ideal" answer should have properly corresponding blank fields (in the sample output, PORTS is empty). The desired output from the question lacks the blank entry for PORTS, which is good because that'd otherwise be impossible to parse out (without changing the docker command or assuming at most one blank entry or having other intimate knowledge of the output possibilities).

Therefore, kottapar's answer, which gives a more appropriate docker command, is vastly preferable.

Adam Katz
  • 951
  • 8
  • 17