以下命令分割文件工作正常,当我为输出文件“打印>“/tmp/outputfile.txt"nfile}'”提供的绝对路径:如何在awk中使用路径变量?
awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--; } print > "/tmp/outputfile.txt"nfile}' inputfile.txt
但是,当我删除了绝对路径,它不工作的变量,我 曾尝试下面的命令 -
printenv |grep tempdir
tempdir=/tmp
awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--; } print > ENVIRON["tempdir"]"outputfile.txt"nfile}' inputfile.txt
awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 -v tempdir="/tmp" '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--; } print > "tempdir/outputfile.txt"nfile}' inputfile.txt
awk -v size=$(wc -l < inputfile.txt) -v perc=0.2 '{nfile = int(NR/(size*perc)); if(nfile >= 1/perc){nfile--; } print > "$tempdir/outputfile.txt"nfile}' inputfile.txt
工作正常,但当我用下面的命令,它工作正常 - awk -v size = $(wc -l <inputfile.txt)-v perc = 0.2 -v tempdir =“/ tmp/output.txt”'{nfile = int(NR /(size * perc) ); if(nfile> = 1/perc){nfile--; }打印> tempdir}'inputfile.txt 内部打印tempdir变量工作正常没有括号,意味着每当我们需要使用连接字符串与打印我们必须使用括号。 –
再次强调,在所有awk版本中,输出重定向右侧的任何表达式都是必需的,以便可移植。换句话说,它在没有它们的情况下可以在一些awk中工作,但不是全部。 –