I'm really new to the programming world. I'm on Windows 10 using Cygwin to run GMT. I'm trying to take an existing file of data and write a new file, editing out rows based on whether the values of two of the columns are greater or less than my intended value. If context helps to make more sense, I'm looking at a set of points on map and I want to take out the ones that do not fit into a certain box of longitudes and latitudes.
Here's a few lines from the file:
01 1 23 29.1 185.8 11.9 3.60
02 1 45 29.2 184.2 -0.5 2.90
03 1 27 28.7 184.8 2.0 2.80
04 1 10 29.3 185.2 2.9 3.60
05 1 30 28.1 185.3 5.0 3.70
06 1 65 29.8 184.9 18.6 3.20
I would like to exclude any row where the 4th column is less than 28.5 and greater than 29.5 and where the 5th column is less than 184.5 and greater than 185.5. I would like the new file to look like this:
03 1 27 28.7 184.8 2.0 2.80
04 1 10 29.3 185.2 2.9 3.60
I have tried this:
#!/bin/bash
while IFS= read -r line;
do
cut -d' ' -f7,8 "$line"
if [ "$4" > "29.5" & "$4" < "28.5" & "$5" < "184.5" & "$5" > "185.5" ]
then
echo $line >> "evlist05.txt"
fi
done < "evlist04.txt"
I first ran it as a .sh script and that gave me:
line 9: syntax error near unexpected token `done'
I then copy and pasted it into Cygwin and it tried to print every line like this:
cut: '01 1 23 29.1 185.8 11.9 3.60'$'\r': No such file or directory
-bash: [: missing `]'
I also tried this:
#!/bin/bash
awk -F " " '{
if [ "$4" > "29.5" & "$4" < "28.5" & "$5" < "184.5" & "$5" > "185.5" ]
{
echo $line >> "evlist05.txt"
}
}'
evlist04.txt
and that told me there's a syntax error on lines 2 and 4. I did struggle to find anything that specifies whether multiple conditions in both an awk command and a conditional statement were possible.
I do apologize if there has already been a similar question asked. I looked around as much as I could, but maybe I'm just not using the right key words. If it has already been asked, I would greatly appreciate being pointed in the right direction.
Thanks in advance for any and all advice!
Aucun commentaire:
Enregistrer un commentaire