Sending output of program into subsequent commands (i.e. awk/sort)

Hi,

I have identified how to use command chaining as per below on a file, to capture the header of a file, as well as the line containing the C: drive.

$ cat test.txt
Filesystem      Size  Used Avail Use% Mounted on
rootfs          237G  153G   84G  65% /
none            237G  153G   84G  65% /dev
none            237G  153G   84G  65% /run
none            237G  153G   84G  65% /run/lock
none            237G  153G   84G  65% /run/shm
none            237G  153G   84G  65% /run/user
C:              237G  153G   84G  65% /mnt/c

$ cat <(head -n1 test.txt) <(awk '/C/' test.txt)
Filesystem      Size  Used Avail Use% Mounted on
C:              237G  153G   84G  65% /mnt/c

I am trying to use the same logic on the output of a command, but it does not work. How can I get this to work?

Aim is to sort docker images by size column on the right, while printing the first line (header).

$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              4d90542f0623        5 days ago          5.58MB
ubuntu              xenial              13c9f1285025        6 days ago          119MB
ubuntu              latest              4c108a37151f        6 days ago          64.2MB
centos              latest              9f38484d220f        3 months ago        202MB
linode/lamp         latest              2359fa12fded        4 years ago         372MB

$ docker image ls | awk 'NR>1' | sort -k5 -V -r
linode/lamp         latest              2359fa12fded        4 years ago         372MB
centos              latest              9f38484d220f        3 months ago        202MB
ubuntu              xenial              13c9f1285025        6 days ago          119MB
ubuntu              latest              4c108a37151f        6 days ago          64.2MB
alpine              latest              4d90542f0623        5 days ago          5.58MB

$ docker image ls | (awk 'NR==1') <(sort -k5 -V -r)
-bash: syntax error near unexpected token `<(sort -k5 -V -r)'  <----- failure
$ docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
alpine              latest              4d90542f0623        5 days ago          5.58MB
ubuntu              xenial              13c9f1285025        6 days ago          119MB
ubuntu              latest              4c108a37151f        6 days ago          64.2MB
centos              latest              9f38484d220f        3 months ago        202MB
linode/lamp         latest              2359fa12fded        4 years ago         372MB

Thanks

Not sure what you're after. Header plus a sorted list of the residual lines? How about

docker image ls | awk 'NR==1 {print; next} {print | "sort -Vrk5"}'

Be aware that sort sees the "days/months/years" field as its key 5.

1 Like

RudiC,
This works perfectly:

$ docker image ls | awk 'NR==1 {print; next} {print | "sort -Vrk5"}'
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
linode/lamp         latest              2359fa12fded        4 years ago         372MB
centos              latest              9f38484d220f        3 months ago        202MB
ubuntu              xenial              13c9f1285025        6 days ago          119MB
ubuntu              latest              4c108a37151f        6 days ago          64.2MB
alpine              latest              4d90542f0623        5 days ago          5.58MB

Is there any way to use command chaining like I used previously for this example? i.e. multiple redirections.

Not with a single input source piped into something. Note that you open and read test.txt twice in your above construct. You could do (but this is far from being smart nor efficient)

cat <(docker image ls | head -n1) <(docker image ls | tail -n+2 | sort -Vrk5 )

You might feel inspired to use tee to split the input into different branches of execution, but it'd be difficult to predict the resultant order of lines. This clumsy approach seems to do it on my system

docker image ls | tee >(head -n1) >(tail -n+2 | sort -rk5) > /dev/null

, but, again, this is far from using common sense.

1 Like

Hi RudiC,

I find it interesting that we are able to push shell commands directly into awk.
Is there any documentation around how this works? Is this only supported with the 'print' statement?

e.g.

awk 'NR==1 {print; next} {print | "sort -Vrk5"}'

Thanks.

Yes, man awk , amongst others:

It is a standard feature of awk for both print and printf .

1 Like

Hi RudiC,

Thanks, interesting to know.