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.
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.
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:
Note that GNU sed accepts
\s
. Not all seds do.The best you can do by editing the output with tools like
sed
is: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 like32 | 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.