What is wrong with my awk code?

Hi there, I am trying to select a number of lines based on the lat. lon columns in a file but my awk code gives me empty result.

Here is my file:

20100213 102212 33.1185 39.4078 2.9
20100214 141753 33.1058 39.9068 2.9
20100218 115828 33.1907 39.3575 2.9
20100225 220001 33.1932 39.9448 2.9
20100226 152045 32.0103 39.3327 2.9
20100228 204455 33.1685 39.3558 2.9
20100323 081029 33.9345 39.9825 2.9
20100328 074927 32.8845 38.7272 2.9
20100330 034504 33.159 38.4325 2.9
20100413 071614 32.959 38.698 2.9
20100427 221931 33.0723 39.4485 2.9
20100524 010333 33.1305 39.3832 2.9
20100928 222019 33.3445 38.938 2.9
20101012 145522 34.0737 38.3135 2.9
20101020 051633 34.7478 38.154 2.9
20101024 232138 34.6318 39.5025 2.9
20101102 141258 32.0048 39.6565 2.9
20110207 140450 33.4383 39.9757 2.9
20110219 140734 34.2425 38.343 2.9
20110227 081313 33.1547 39.3738 2.9

3rd column = LON, 4th column = LAT

I need to select events between 39.10 <= $4 <= 39.64 and 32.75 <= $3 <= 33.28

I know there are lines that satisfy these conditions in the files.

and my code:

cat latlon.dat | awk '($4 >= 39.10 && $4 <= 39.64) && ($3 >= 32.75 && $3 <= 33.28) {print $0}'

This just gives empty list.

What is your OS and version? I get:

20100213 102212 33.1185 39.4078 2.9
20100218 115828 33.1907 39.3575 2.9
20100228 204455 33.1685 39.3558 2.9
20100427 221931 33.0723 39.4485 2.9
20100524 010333 33.1305 39.3832 2.9
20110227 081313 33.1547 39.3738 2.9

Centos 5.8 and GNU Awk 3.1.5

Hi.

Here are results from a CentOS / awk combination:

#!/usr/bin/env bash

# @(#) s1	Demonstrate arithmetic comparison, and filtering, awk.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C awk

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
awk '($4 >= 39.10 && $4 <= 39.64) && ($3 >= 32.75 && $3 <= 33.28) {print $0}' $FILE

exit 0

producing:

$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.32-358.23.2.el6.centos.plus.x86_64, x86_64
Distribution        : CentOS 6.4 (Final)
bash GNU bash 4.1.2
awk GNU Awk 3.1.7

-----
 Input data file data1:
20100213 102212 33.1185 39.4078 2.9
20100214 141753 33.1058 39.9068 2.9
20100218 115828 33.1907 39.3575 2.9
20100225 220001 33.1932 39.9448 2.9
20100226 152045 32.0103 39.3327 2.9
20100228 204455 33.1685 39.3558 2.9
20100323 081029 33.9345 39.9825 2.9
20100328 074927 32.8845 38.7272 2.9
20100330 034504 33.159 38.4325 2.9
20100413 071614 32.959 38.698 2.9
20100427 221931 33.0723 39.4485 2.9
20100524 010333 33.1305 39.3832 2.9
20100928 222019 33.3445 38.938 2.9
20101012 145522 34.0737 38.3135 2.9
20101020 051633 34.7478 38.154 2.9
20101024 232138 34.6318 39.5025 2.9
20101102 141258 32.0048 39.6565 2.9
20110207 140450 33.4383 39.9757 2.9
20110219 140734 34.2425 38.343 2.9
20110227 081313 33.1547 39.3738 2.9

-----
 Results:
20100213 102212 33.1185 39.4078 2.9
20100218 115828 33.1907 39.3575 2.9
20100228 204455 33.1685 39.3558 2.9
20100427 221931 33.0723 39.4485 2.9
20100524 010333 33.1305 39.3832 2.9
20110227 081313 33.1547 39.3738 2.9

I don't have a version of CentOS older than this.

Best wishes ... cheers, drl

Hmm, interesting. I have tried the code on Ubuntu machine and it works. I wonder what is the problem with my Centos workstation.

What happens if you copy-paste the file content hat you posted here into a new file on your original CentOS 5.8 host and try again with this new file?

I also got suspicious that maybe my file was damaged (modified) somehow and copy/pasted the contents to a new file as you suggested, but it still doesn't work on Centos 5.8. :confused:

Alright, just to make sure, could can you copy and paste this code right onto your bash command line ?

awk '($4 >= 39.10 && $4 <= 39.64) && ($3 >= 32.75 && $3 <= 33.28) {print $0}' << "EOF"
20100213 102212 33.1185 39.4078 2.9
20100214 141753 33.1058 39.9068 2.9
20100218 115828 33.1907 39.3575 2.9
20100225 220001 33.1932 39.9448 2.9
20100226 152045 32.0103 39.3327 2.9
20100228 204455 33.1685 39.3558 2.9
20100323 081029 33.9345 39.9825 2.9
20100328 074927 32.8845 38.7272 2.9
20100330 034504 33.159 38.4325 2.9
20100413 071614 32.959 38.698 2.9
20100427 221931 33.0723 39.4485 2.9
20100524 010333 33.1305 39.3832 2.9
20100928 222019 33.3445 38.938 2.9
20101012 145522 34.0737 38.3135 2.9
20101020 051633 34.7478 38.154 2.9
20101024 232138 34.6318 39.5025 2.9
20101102 141258 32.0048 39.6565 2.9
20110207 140450 33.4383 39.9757 2.9
20110219 140734 34.2425 38.343 2.9
20110227 081313 33.1547 39.3738 2.9
EOF

It should produce this:

20100213 102212 33.1185 39.4078 2.9
20100218 115828 33.1907 39.3575 2.9
20100228 204455 33.1685 39.3558 2.9
20100427 221931 33.0723 39.4485 2.9
20100524 010333 33.1305 39.3832 2.9
20110227 081313 33.1547 39.3738 2.9

Sorry for my late reply due to weekend.

I have noticed that this problem happens when I connect to Centos machine through ssh from my ubuntu desktop (That is how I work). Copying and pating into terminal as you suggested also not working.

However, when I run the code on the terminal of the Centos machine it is working!

Later on I tried to ssh from my Windows machine to Centos in order to see if the problem caused by SSH. To my suprise the code works without a problem!

So in summary, it seems my awk code doesn't work when I connect to Centos machine via SSH from my Ubuntu desktop. It is strange. By the way the code works perfectly on ubuntu itself :slight_smile:

So do you guys have any theories about this?

Interesting. Maybe a TERM setting? What does $TERM contain before and after you ssh from your Ubuntu desktop to Centos? What desktop are you using on Ubuntu?

I am using unity as a desktop on my Ubuntu 14.04. TERM variable shows "xterm" before and after.