with regex, why am i unable to get multiple lines to match in linux

Viewed 51

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)'

1 Answers

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.*]'
Related