I have a gridded model data (separated by different depth) saved in text files. The structure is like this:
Format of every column in a row in the text file:
x_coordinate y_coordinate density
There are about 400*400 points in every text file(as a plane in certain depth).
Filename:
dep###
Here, ###
is a number represents the depth(in Z direction). The number can be an integer or fraction. For example, now I have these files: "dep0", "dep0.5", "dep10", "dep300", that means those data are 400*400 gridded data in x-y plane form for depth of 0, 0.5, 10 and 300.
Now, I would like to pick up the number in the filename (aka the depth) and add it to the third column of each row, combine all of them together. Also the depth should be sorted from minima to maxima. So the output file should looks like this(for example):
x_coordinate y_coordinate z_coordinate density 0 0 0 2.5 0 1 0 2.5 ... ... 0 2.6 400 400 0 2.9 0 0 0.5 2.8 ... ... 0.5 2.9 0 0 10 3.2 ... ... 10 3.3 ... ... 300 4.7 ... ... 300 4.8
At first I was doing it with this script:
for((i=$depmin;i<=$depmax;i++)) do if [ -f "xyp/dep"$i ];then awk '{print $1, $2,'$i',$3}' "xyp/dep"$i >> "xyzp/area1" fi done
Then I figure out it would miss any files which depth is not integer, since the variable $i in the for
loop increase by 1 in every round.
I have tried to use sed
and find -exec
, but I kept getting errors. The difficulty for me is that I don't quite understand how to use $
, ''
, <<<
correctly to redirect or pipe the value to awk
or other function. Please help me with this problem.
=====================
I have come up with this script:
depnumbers=$(ls xyp | sed -e 's/dep//g' |sort -n) filecount=$(ls xyp | wc -l) for((i=1;i<=$filecount;i++)) do dep=$(awk '{print $'$i'}' <<< $depnumbers) awk '{print $1, $2,'$dep',$3}' "xyp/dep"$dep >> "xyzp/area1" done
It works just fine. Is there any way to simplify or improve this script for such task? Actually I'm new to bash and still think something is wrong...not sure