1

i would like to get everything that is inside the CreateCommand below:

so, i've tried CreateCommand(.|\n)*], on the regex101 site:

            "CreateCommand": [
                "docker",
                "run",
                "-p",
                "3000:3000",
                "-v",
                "/home/blah:/usr/src/app",
                "-w",
                "/usr/src/app",
                "--name",
                "dev",
                "node:18-bullseye-slim",
                "npm",
                "run",
                "dev"
            ],
"Umask": "0022"

and it works great.

Then, i try docker inspect dev | grep 'CreateCommand(.|\n)*],' and i get nothing.

I also tried:

docker inspect dev > insp.txt
cat insp.txt | grep 'CreateCommand(.|\n)*],'

so, it seems like i'm missing some kind of nuance here... because this gives me at least something:

cat insp.txt | grep 'CreateCommand.*' --> "CreateCommand": [

edit:

i have tried a few different variations, including taking off some from the right side:

cat insp.txt | grep -E 'CreateCommand((.|\n)*).*(?="Umask)'

avnav99
  • 532
  • 5
  • 16

1 Answers1

1

grep operates line by line; no regex will work.

Convert the output to a single line using tr to delete newlines, then use grep:

docker inspect dev | tr -d '\n' | grep 'CreateCommand.*]'
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • ok i did not realize it is line by line. here are some ways i was able to do it: `cat insp.txt | tr -d '\n\r\t ' | grep -oP '(CreateCommand").*(?="Umask)'` or `podman inspect dev | python -c 'import json, sys; print(" ".join(json.loads(sys.stdin.read())[0]["Config"]["CreateCommand"]).encode("utf-8") );'` – avnav99 Aug 28 '22 at 21:54