awk:致命:无法打开文件“"进行读取(没有这样的文件或目录) [英] awk: fatal: cannot open file `' for reading (No such file or directory)

查看:261
本文介绍了awk:致命:无法打开文件“"进行读取(没有这样的文件或目录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网格中的节点读取x和y坐标.所有节点的坐标都在文件mesh_coords.xyz中.我想要一个引用行1055的行,该行引用一个叫做Jalisco的地方.

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.

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

jalisco=`awk '{if (NR==1055) print $0}' ${nodes_file}`

x=`awk '{print $1}' ${jalisco}`
y=`awk '{print $2}' ${jalisco}`

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

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).

但是:

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

awk '{if (NR==1055) print $0}' ${nodes_file}

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

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

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

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

推荐答案

我相信 $ jalisco 变量将x-y坐标保存为字符串中的空格.显然 $ jalisco 不是文件,因此您的最后2个awk命令给出了错误.

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

您可以使用此:

x=$(awk '{print $1}' <<< "${jalisco}")
y=$(awk '{print $2}' <<< "${jalisco}")

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

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

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

还请注意,您的 awk 命令可以简化为:

Also note that your awk command can be shortened to just:

awk 'NR==1055' "$nodes_file"

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

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

这篇关于awk:致命:无法打开文件“"进行读取(没有这样的文件或目录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆