Skip to content

Instantly share code, notes, and snippets.

@ramkumardevanathan
Created November 25, 2014 03:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ramkumardevanathan/d25bb91d8ab170d3b656 to your computer and use it in GitHub Desktop.
Save ramkumardevanathan/d25bb91d8ab170d3b656 to your computer and use it in GitHub Desktop.
simple gist to get docker ps output as csv file
docker ps -a --no-trunc | awk -F " +" '{$1=$1}1' OFS="\t"
@matteo-bombelli
Copy link

matteo-bombelli commented Apr 5, 2020

hey very good!
but this is a tsv (tab separated values), csv (comma separated values) is:
docker ps -a --no-trunc | awk -F " +" '{$1=$1}1' OFS=","
but thank you very much!

@frakman1
Copy link

frakman1 commented Aug 3, 2020

@matteo-bombelli
There is a bug in your script. CONTAINER ID turns into CONTAINER,ID which is undesireable
Also, the created fields like "5 weeks ago" becomes three different fields (5,weeks,ago)

@e-ruiz
Copy link

e-ruiz commented Aug 1, 2023

@frakman1

This is the trick --format with \t on docker container ls and grab that on awk with -F '\t'

docker container ls --no-trunc -a --format='"{{.Names}}"\t"{{.Image}}"\t"{{.Ports}}"\t"{{.Status}}"' | awk -F '\t' '{$1=$1}1' OFS=","

Note that quoted values provided from --format, good to avoid undesired fields split.

Of course a better way is without awk, in this case a simple:

docker container ls --no-trunc -a --format='"{{.Names}}","{{.Image}}","{{.Ports}}","{{.Status}}"'

References:
https://docs.docker.com/engine/reference/commandline/ps/#format
https://stackoverflow.com/questions/5374239/tab-separated-values-in-awk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment