bash awk: 致命: 无法打开文件 `' 进行读取(没有这样的文件或目录)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26816605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:45:34  来源:igfitidea点击:

awk: fatal: cannot open file `' for reading (No such file or directory)

linuxbashshellawk

提问by co323

I'm trying to read x and y coordinates from a node in a grid. The coordinates of all the nodes are in the file mesh_coords.xyz. I want the one referring to line 1055, which refers to a place called Jalisco.

我正在尝试从网格中的节点读取 x 和 y 坐标。所有节点的坐标都在文件mesh_coords.xyz 中。我想要引用第 1055 行的那个,它指的是一个叫哈利斯科州的地方。

nodes_file='../output/ascii/mesh_coords.xyz'

jalisco=`awk '{if (NR==1055) print 
nodes_file='../output/ascii/mesh_coords.xyz'

awk '{if (NR==1055) print 
x=$(awk '{print }' <<< "${jalisco}")
y=$(awk '{print }' <<< "${jalisco}")
}' ${nodes_file}
}' ${nodes_file}` x=`awk '{print }' ${jalisco}` y=`awk '{print }' ${jalisco}`

Returns: "awk: cmd. line:1: fatal: cannot open file `4250.000000' for reading (No such file or directory)" twice (I assume once for x and once for y).

返回:“awk:cmd。行:1:致命:无法打开文件‘4250.000000’进行读取(没有这样的文件或目录)”两次(我假设一次是x,一次是y)。

However:

然而:

read x y < <(awk 'NR==1055' "$nodes_file")

prints the correct x and y coordinates. I need to use the variables x and y later so they need to be set properly.

打印正确的 x 和 y 坐标。我稍后需要使用变量 x 和 y,因此需要正确设置它们。

I'm relatively new to Linux so apologies if this is a simple awk/shell syntax problem.

我对 Linux 比较陌生,如果这是一个简单的 awk/shell 语法问题,我深表歉意。

回答by anubhava

I believe the $jaliscovariable is holding x-y co-ordinates separated by space in a string. Obviously $jaliscois not a file hence your last 2 awk commands are giving errors.

我相信该$jalisco变量在字符串中保存了由空格分隔的 xy 坐标。显然$jalisco不是文件,因此您的最后 2 个 awk 命令给出了错误。

You can use this:

你可以使用这个:

awk 'NR==1055' "$nodes_file"

Or better yet, get both values from your first awk itself using process substitution:

或者更好的是,使用进程替换从您的第一个 awk 本身获取两个值:

##代码##

Also note that your awkcommand can be shortened to just:

另请注意,您的awk命令可以缩短为:

##代码##

The default action is to print the line, so this is what awk will do when the condition NR==1055is true.

默认操作是打印该行,因此当条件NR==1055为真时 awk 将执行此操作。