0

I have a function that takes a filename. It then runs a command and filters the output (exclusive) between two patterns, then it outputs comma separated values with the filename output of the command.

Here is the function and the expected output:

get_cons_obs() {
    local line="${1}"
    "command" -i "${line}" 2>&1 \
        | awk '/^ERROR$/{print "ERROR"} /^START$/{flag=1;next} /^END$/{flag=0} flag' \
        | xargs printf "${line},%s\n"
}
file01,thing01
file01,thing02
file01,thing03
.
.
.

Is it possible to combine awk command and the xargs printf command? I can't seem to append the "flagged" lines with the $line variable.

dylanjm
  • 201

2 Answers2

1

It sounds like you want to pass the shell variable line into awk so that you can print it when flag is non-zero

Ex.

awk -v line="$line" '. . . flag {printf "%s,%s\n", line, $0}'

See also

steeldriver
  • 81,074
0

Tried with Below command and it worked fine

awk '/pattern1/,/pattern2/{print $0","FILENAME}' filename
bu5hman
  • 4,756