Search code examples
awkdockerfish

How to escape Fish shell variable to use as awk pattern?


I'm writing a function to list my latest images from a given docker repository:

function docker-latest
      set repo $argv[1]
      docker images | awk "/$repo/ && /latest/{print $1}"
  end

Works fine…

Here is the docker images output

$ docker images
REPOSITORY                                       TAG                 IMAGE ID            CREATED             SIZE
coaxisopt_daemon                                 latest              86bd3d602074        17 hours ago        830.7 MB
coaxisopt_daemon                                 v1.0.13             86bd3d602074        17 hours ago        830.7 MB
docker.site.fr:5000/coaxis/coaxisopt_daemon     latest              86bd3d602074        17 hours ago        830.7 MB
<none>                                           <none>              da0e5b0fc2a1        17 hours ago        830.7 MB
docker.site.fr:5000/coaxis/coaxisopt_daemon     <none>              9c0175d7d397        18 hours ago        830.7 MB
…

Here is my output as expected:

$ docker-latest coaxis
coaxisopt_daemon                                 latest              86bd3d602074        17 hours ago        830.7 MB
docker.akema.fr:5000/coaxis/coaxisopt_daemon     latest              86bd3d602074        17 hours ago        830.7 MB

Until

However, when I put some / (slash) character in the end of my string to filter on pushed images:

$ docker-latest coaxis/
awk: cmd. line:1: /coaxis// && /latest/{print }
awk: cmd. line:1:           ^ syntax error

Question

How do I escape the repo variable so I used it safely in awk pattern?


Solution

  • Solution

    docker images | awk -v repo="$repo"  '$1 ~ repo && $2 == "latest" {print $1}'
    

    Details: the trick is to pass the $repo through the awk's variable repo and escape $1.