co3*_*323 2 linux bash shell awk
我正在尝试从网格中的节点读取x和y坐标.所有节点的坐标都在文件mesh_coords.xyz中.我想要一个引用1055行的那个,它指的是一个叫做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}`
Run Code Online (Sandbox Code Playgroud)
返回:"awk:cmd.行:1:致命:无法打开文件`4250.000000'进行读取(没有这样的文件或目录)"两次(我假设一次为x,一次为y).
然而:
nodes_file='../output/ascii/mesh_coords.xyz'
awk '{if (NR==1055) print $0}' ${nodes_file}
Run Code Online (Sandbox Code Playgroud)
打印正确的x和y坐标.我需要稍后使用变量x和y,因此需要正确设置它们.
我对Linux比较陌生,所以如果这是一个简单的awk/shell语法问题,请道歉.
我相信$jalisco变量是用字符串中的空格分隔xy坐标.显然$jalisco不是文件因此你的最后2个awk命令给出了错误.
你可以用这个:
x=$(awk '{print $1}' <<< "${jalisco}")
y=$(awk '{print $2}' <<< "${jalisco}")
Run Code Online (Sandbox Code Playgroud)
或者更好的是,使用进程替换从第一个awk本身获取两个值:
read x y < <(awk 'NR==1055' "$nodes_file")
Run Code Online (Sandbox Code Playgroud)
另请注意,您的awk命令可以缩短为:
awk 'NR==1055' "$nodes_file"
Run Code Online (Sandbox Code Playgroud)
默认操作是打印该行,因此这是条件NR==1055为真时awk将执行的操作.