Search code examples
shellawkshindentation

Is it possible to split an AWK command in multiple lines, without having an error?


I've an AWK command that is pretty long

awk -v AWK_SECTION_TITLE="$SECTION_TITLE" -v AWK_ADDED="$ADDED" -v AWK_CHANGED="$CHANGED" -v AWK_FIXED="$FIXED" -v AWK_REMOVED="$REMOVED" 'NR=='"$CHANGELOG_INSERT_LINE"'{
    print AWK_SECTION_TITLE
    ....

I would like to know if it is possible to split this first part -where I set the variables for awk scope- into multi lines, with some backslashes or any other syntax.

I tried this approach:

awk
    \ -v AWK_SECTION_TITLE="$SECTION_TITLE"
    \ -v AWK_ADDED="$ADDED"
    ...

and this was the error

usage: awk [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]
script.sh: line 65:  -v: command not found

I tried this approach:

awk -v AWK_SECTION_TITLE="$SECTION_TITLE"
    \ -v AWK_ADDED="$ADDED" -v AWK_CHANGED="$CHANGED"

and this was the error

awk: no program given

I tried this approach:

awk -v AWK_SECTION_TITLE="$SECTION_TITLE"
    \ -v AWK_ADDED="$ADDED" -v AWK_CHANGED="$CHANGED"

and this was the error

awk: no program given

Thanks in advance.


Solution

  • When you add an escape it's to escape the character that follows it. Putting it at the start of a line escapes whatever character follows it on that line instead of the command-terminating newline you want to escape at the end of the preceding line.

    Do:

    cmd foo \
        bar
    

    instead of:

    cmd foo
    \    bar