我试图找到具有给定扩展名的所有文件,并 awk 结果并将其附加到 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
我能够从 find 命令获取输出,
.fielename.gz Jason 197025960 May 2018
但是,我从 awk 命令中收到此语法错误,
+ awk -vextension=gzip -vfiletype=gz -vcompress=compressed '-vOFS=\t' '{print $0, extension, filetype, compress}'
awk: {print $0, extension, filetype, compress}
awk: ^ syntax error
答案1
尝试extension
用其他名称替换变量名称,例如ext
; in gawk
,extension
是关键字/内置。
$ 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