Reputation: 37
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}`
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).
However:
nodes_file='../output/ascii/mesh_coords.xyz'
awk '{if (NR==1055) print $0}' ${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.
I'm relatively new to Linux so apologies if this is a simple awk/shell syntax problem.
Upvotes: 2
Views: 35709
Reputation: 785531
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.
You can use this:
x=$(awk '{print $1}' <<< "${jalisco}")
y=$(awk '{print $2}' <<< "${jalisco}")
Or better yet, get both values from your first awk itself using process substitution:
read x y < <(awk 'NR==1055' "$nodes_file")
Also note that your awk
command can be shortened to just:
awk 'NR==1055' "$nodes_file"
The default action is to print the line, so this is what awk will do when the condition NR==1055
is true.
Upvotes: 7