2

I have a template config file with PlaceHolders, and i want to find all those PlaceHolders and put inside an array.

The curent state:
I can find all the PlaceHolders in file only if there is no more than one PlaceHolder in a line.

For example, this is my template file:

upstream portal {        
   server {{UNICORN_SERVICE_NAME}}:{{UNICORN_SERVICE_PORT}};  
}

server {
  listen *:80 default_server;         
  server_name {{APP_HOST}};     
  server_tokens off;     
  root /dev/null;

  # Increase this if you want to upload large attachments
  # Or if you want to accept large git objects over http
  client_max_body_size {{NGINX_MAX_UPLOAD_SIZE}};

  location {{GITLAB_RELATIVE_URL_ROOT}}/ {
    root /var/lib/nginx/portal;
    # serve static files from defined root folder;.
    # @gitlab is a named location for the upstream fallback, see below    
  }
  }

this is the code i use to find the PlaceHolders:

matches_bloc=$(awk 'match($0, /(\{\{)([^{]*)(\}\})/) {
                    print substr($0, RSTART, RLENGTH)                    
                }' ${currFile})

            # convert 'matches_bloc' into array
            matches=()
            echo "Matches:"
            while read -r line; do
                matches+=("$line")
                echo "  - ${line}"
            done <<< "$matches_bloc"

in this example the matches result will be:

Matches:
- {{UNICORN_SERVICE_NAME}}
- {{APP_HOST}}
- {{NGINX_MAX_UPLOAD_SIZE}}
- {{GITLAB_RELATIVE_URL_ROOT}}

You can notice that there are 5 PlaceHolders in the file and only 4 matches.
The missing match is: {{UNICORN_SERVICE_PORT}}, because there is already another match in the same line.

My question is:
How can i find all the matches in the file regardless of the line?

1 Answer 1

6

Find all variables in a template file and put them in an array.

With GNU grep:

array=( $(grep -Po '{{.*?}}' file) )
declare -p array

Output:

declare -a array='([0]="{{UNICORN_SERVICE_NAME}}" [1]="{{UNICORN_SERVICE_PORT}}" [2]="{{APP_HOST}}" [3]="{{NGINX_MAX_UPLOAD_SIZE}}" [4]="{{GITLAB_RELATIVE_URL_ROOT}}")'

-P: Interpret {{.*?}} as a Perl regular expression.

-o: Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

* The preceding expression can match zero or more times. With ? the * tries to match as few as possible (non-greedy).


See: The Stack Overflow Regular Expressions FAQ

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you cyrus for great code, could you please explain the regex part, will be grateful to you sir.
can be done without -P as well... grep -o '{{[^}]*}}' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.