Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要bash的awk变量_Bash_File_Awk_Syntax Error - Fatal编程技术网

要bash的awk变量

要bash的awk变量,bash,file,awk,syntax-error,Bash,File,Awk,Syntax Error,我有一个包含5列的文件,希望将其用作bash中的变量。当我只想读取一个变量时,例如从file.dat第3列中的变量读取时,我使用 var=$(awk 'print($3)' file.dat) 我想读取最后三列并将其保存到数组或多个变量中,是否可能?也许有一个更简单的方法听起来像这样的问题:。 在您的情况下,您可以尝试以下方法: awk '{ print($1 " " $2 " " $3 " " $4 " " $5); }' file.dat | { read a b c d e; echo

我有一个包含5列的文件,希望将其用作bash中的变量。当我只想读取一个变量时,例如从
file.dat
第3列中的变量读取时,我使用

var=$(awk 'print($3)' file.dat)

我想读取最后三列并将其保存到数组或多个变量中,是否可能?也许有一个更简单的方法

听起来像这样的问题:。 在您的情况下,您可以尝试以下方法:

awk '{ print($1 " " $2 " " $3 " " $4 " " $5); }' file.dat | { read a b c d e; echo $a $b $c $d $e; }
或者,如果需要从file.dat循环行:

awk '{ print( $1 " " $2 " " $3 " " $4 " " $5); }' file.dat | \
{ while read a b c d e; do echo $a $b $c $d $e; done; }

请将示例输入(无描述、无图像、无链接)和该示例输入的所需输出添加到您的问题中(无注释)。您是否计划编写脚本,以便使用
array=($(script-c3,4,5 file.dat))
分配给数组,还是每次
array=($(awk'{print$3,$4,$5}'file.dat)
还是什么?通过一次调用
awk
保存到多个变量更难;你最终会做类似于
eval
的事情,这是危险的(安全风险),但可行。
awk'print($3)'file.dat
我怀疑您是否通过这条awk线得到了正确的结果。这是否回答了您的问题?在问题的每一点上,将“变量”和“数组”更改为“awk变量”或“外壳变量”和“awk数组”或“外壳数组”,以澄清您试图引用/填充的内容。