2

I have to extract latitude and longitude from an ASCII text file in Ubuntu using the awk command. I wrote the code below and saved it in untitled.sh:

#!/bin/csh -f
set file =  $1
if($1! = "") then
awk'{ \
if(NR == 1) NCOLS = $2; \
if(NR == 2) NROWS = $2; \
if(NR == 3) LL_LNG = $2; \
if(NR == 4) LL_LAT = $2; \
if(NR == 5) CELLSIZE = $2; \
if(NR == 6) NODATA = $2; \
if(NR > 6) { \
for(i = 1; i <= NF; i++ ) { \
  if( $i!= NODATA ) { \
    printf( "%f %f %f\n", LL_LNG + (i-1) * CELLSIZE + CELLSIZE / 2, LL_LAT + (NROWS-(NR-7)) *    CELLSIZE - CELLSIZE / 2, $i ) \
#printf( "%f %f %f\n" LL_LNG + (i-1) * CELLSIZE, LL_LAT + (NROWS-(NR-7)) * CELLSIZE, $i ) \
     } \
   } \
  } \
 } '$file 

After that I run it by csh untitled.sh or csh csh.sh test.txt, but it is telling:

 if: Expression Syntax.

How to solve it?

Byte Commander
  • 110,243

2 Answers2

2

Syntax issue, you need endif for your if if($1! = "") then + this if statement should be if ($1 != "") then.

αғsнιη
  • 36,350
2

You have two major issues here. First, you are using csh for scripting. That is a very bad idea. I suggest you read Tom Christiansen's great essay Csh Programming Considered Harmful to understand why this should be avoided. Csh is fine as a shell, but really shouldn't be used for scripting.

Now, the reason this isn't working is the syntax error. You need != and not ! =. Strictly speaking, you also need an endif but the script will work without it since there are no operations outside the if block. It will break if you try to do anything else though, so you really should have it.

Also, your script will break if you try to run it on a file whose name contains spaces because you're not quoting your variables. With all this in mind, here's a working version of your script:

#!/bin/csh -f
set file =  "$1"
if("$file" != "") then
awk '{ \
if(NR == 1) NCOLS = $2; \
if(NR == 2) NROWS = $2; \
if(NR == 3) LL_LNG = $2; \
if(NR == 4) LL_LAT = $2; \
if(NR == 5) CELLSIZE = $2; \
if(NR == 6) NODATA = $2; \
if(NR > 6) { \
for(i = 1; i <= NF; i++ ) { \
  if( $i!= NODATA ) { \
    printf( "%f %f %f\n", LL_LNG + (i-1) * CELLSIZE + CELLSIZE / 2, LL_LAT + (NROWS-(NR-7)) *    CELLSIZE - CELLSIZE / 2, $i ) \
#printf( "%f %f %f\n" LL_LNG + (i-1) * CELLSIZE, LL_LAT + (NROWS-(NR-7)) * CELLSIZE, $i ) \
     } \
   } \
  } \
 } ' "$file"
endif

Of course, if this is all you want to do, you could make your life easier and just use awk directly instead of fiddling around with csh:

#!/usr/bin/awk -f
{ 
    if(NR == 1) NCOLS = $2; 
    if(NR == 2) NROWS = $2; 
    if(NR == 3) LL_LNG = $2;
    if(NR == 4) LL_LAT = $2;
    if(NR == 5) CELLSIZE = $2; 
    if(NR == 6) NODATA = $2; 
    if(NR > 6) { 
        for(i = 1; i <= NF; i++ ) { 
            if( $i!= NODATA ) { 
                printf( "%f %f %f\n", LL_LNG + (i-1) * CELLSIZE + CELLSIZE / 2, LL_LAT + (NROWS-(NR-7)) *    CELLSIZE - CELLSIZE / 2, $i ) 
            } 
        } 
  } 
}

Save that as foo.awk, make it executable, and run it on your file:

./foo.awk file
Eliah Kagan
  • 119,640
terdon
  • 104,119