0

I need to insert an awk command in a shell script like this:

#/bin/bash
temp_folder="FOO"
awk 'BEGIN {
    RS="END:VCARD"
    FS="\n"
}
{
    command = "echo -n FOO/$(pwgen 20 1).vcf"
    command | getline filename
    close(command)
    print $0 "END:VCARD" > filename
    close(filename)
}' "${file}"

PROBLEM: In the BEGIN section, I want to replace FOO by the temp_folder variable (like $temp_folder, ${temp_folder} or something else). I have tried different solution but none worked to make the value of the variable recognized. (Problem with quotes?).

How to write the variable in the section ?

  • 1
    You can't use a shell variable in a single-quoted argument including an awk script; if you double-quote the script $temp_folder works but the $(cmd) and $0 already in the script need to be backslashed. If you make it an _environment_ variable instead (with `export`, `declare/typeset -x`, or `set -a`) you can access it in awk with `ENVIRON["name"]`, or if you make it an _awk_ variable with `-v name="value"` you can use that in awk as-is. Aside: you aren't using FOO in the BEGIN rule, only in the second 'all-records' rule. – dave_thompson_085 Aug 04 '23 at 02:30

0 Answers0