0

I am trying to to find all the files with a given extension and awk the results and append into all.txt.

set -xe

EXT="${1}"
TYPE="${2}"
COMP="${3}"

find -iname \*.${EXT} -print0 | xargs -0 ls -l --time-style="+%b %Y" | awk '{print $NF,$3,$5,$6,$7}' OFS="\t" > ${EXT}.txt
awk -vextension="${EXT}" -vfiletype="${TYPE}" -vcompress="${COMP}" -vOFS="\t" '{print $0, extension, filetype, compress}' < ${EXT}.txt >> all.txt

I am able to get the output from find command,

.fielename.gz   Jason   197025960   May 2018

However, I am getting this syntax error from awk command,

+ awk -vextension=gzip -vfiletype=gz -vcompress=compressed '-vOFS=\t' '{print $0, extension, filetype, compress}'
awk: {print $0, extension, filetype, compress}
awk:                     ^ syntax error
1
  • Unable to replicate, I get no syntax error when attempting to run that awk invocation.
    – DopeGhoti
    Commented Sep 20, 2018 at 16:40

1 Answer 1

2

Try replacing the extension variable name with something else, eg ext; in gawk, extension is a keyword / built-in.

$ gawk 'BEGIN{print extension}'
gawk: cmd. line:1: BEGIN{print extension}
gawk: cmd. line:1:                      ^ syntax error
$ gawk 'BEGIN{print ext}'  # OK
^D
$ gawk -vextension=gzip 'BEGIN{print extension}'
gawk: fatal: cannot use gawk builtin `extension' as variable name

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .