Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

readout data into a new file 1

Status
Not open for further replies.

tini1208

Technical User
Feb 13, 2006
56
DE
hi at all,

now, i have a new problem and started to write a script, but there is an error and i don't where.

the file looks like this:

i_lat = 214748 214748 214748 214748
214748 214748 214748 214748 214748 214748
214748 214748 214748 214748 214748 214748
214748 214748 214748 214748 792766 214748
214748 792809 792823 792837 214748 792866
792880 792894 792908 792923 214748 792951
214748 214748 792994 793009 793023 793037

i_lon = 214748 214748 214748 214748
214748 214748 214748 214748 214748 214748
214748 214748 214748 214748 214748 214748
214748 214748 214748 214748 193651 214748
214748 193642 193638 193635 214748 193628
193625 193622 193619 193615 214748 193609
214748 214748 193599 193596 193593 193590

now, i want to print all the data among each other, like this:

i_lat i_lon
xxxxx xxxxx
xxxxx xxxxx
xxxxx xxxxx
xxxxx xxxxx
xxxxx xxxxx
xxxxx xxxxx

my trying is this:

BEGIN {
print "lat lon"
}
$1 == "i_lat" {
lat[0]=$3
lat[1]=$4
lat[2]=$5
lat[3]=$6
for (i=4; i<40; i++) {
getline
lat=$1
lat=$2
lat=$3
lat=$4
lat=$5
lat=$6
}
for (i=10; i<40; i++) {
getline
lat=$1
lat=$2
lat=$3
lat=$4
lat=$5
lat=$6
}
for (i=16; i<40; i++){
getline
lat=$1
lat=$2
lat=$3
lat=$4
lat=$5
lat=$6
}
for (i=22; i<40; i++){
getline
lat=$1
lat=$2
lat=$3
lat=$4
lat=$5
lat=$6
}
for (i=28; i<40; i++){
getline
lat=$1
lat=$2
lat=$3
lat=$4
lat=$5
lat=$6
}
for (i=34; i<40; i++){
getline
lat=$1
lat=$2
lat=$3
lat=$4
lat=$5
lat=$6
}
$1 == "i_lon" {
lon[0]=$3
lon[1]=$4
lon[2]=$5
lon[3]=$6
for (i=4; i<40; i++) {
getline
lon=$1
lon=$2
lon=$3
lon=$4
lon=$5
lon=$6
}
for (i=10; i<40; i++) {
getline
lon=$1
lon=$2
lon=$3
lon=$4
lon=$5
lon=$6
}
for (i=16; i<40; i++){
getline
lon=$1
lon=$2
lon=$3
lon=$4
lon=$5
lon=$6
}
for (i=22; i<40; i++){
getline
lon=$1
lon=$2
lon=$3
lon=$4
lon=$5
lon=$6
}
for (i=28; i<40; i++){
getline
lon=$1
lon=$2
lon=$3
lon=$4
lon=$5
lon=$6
}
for (i=34; i<40; i++){
getline
lon=$1
lon=$2
lon=$3
lon=$4
lon=$5
lon=$6
}
for (i=0; i<40; i++) {
print lat lon
}
}

awk says the error is in line: for (i=0; i<40; i++){, but when i command this line out, it also doesn't work.

has anybody an idea?

thanks...tini

 
Hi

Do yourself a favor. Indent your code consistently and insert empty lines between important blocks.
Code:
[gray]# ...[/gray]
    lat[i]=$5
    lat[i]=$6
    }
[red]}[/red]
$1 == "i_lon" {
        lon[0]=$3
        lon[1]=$4
[gray]# ...[/gray]

Feherke.
 
Try this shorter solution:

Code:
awk '
        # start from first field
        { f=1 }
        # unless there is an i_, in which case
        # start from third field, reset count
        /^i_/ { f=3; x=$1; y=0 }
        {
                # add each field to array
                for (;f<=NF;f++) { a[x,y++]=$f }
                # record total number of fields
                if (y > ymax) { ymax=y }
        }
        END {
                print "i_lat","i_lon"
                for (i=0; i<ymax; i++) {
                        print a["i_lat",i],a["i_lon",i]
                }
        }
'

Annihilannic.
 
hi

@feherke: thanks for the advise, you are definitely right. i insert empty lines and tried to make my code consistent. but what do you exactly mean with the red bracket? shall i remove it?

@annihilannic: thanks also for your shorter solution, but i got a few error massages. in line 9

(for (;f<=NF;f++) { a[x,y++]=$f })

is an error and i removed the first semicolon. is this right? i don't think so, because afterwards, i get error massages for line 9 (again - syntax error), 10, 12 and 13 (all three illegal statements).
there is also an error massage (both syntax and illegal statement) in line 16

(print a["i_lat",i],a["i_lon",i]),

but i can't find anything.

sorry for my ignorance.

cheers,
tini
 
tini1208 said:
@annihilannic: thanks also for your shorter solution, but i got a few error massages. in line 9

Can you paste the actual error messages?

You are correct, the semi-colon does need to be there. Normally you would initialise the counter in there (e.g. f=1), but because I have already initialised it earlier in the code I left it out.

Annihilannic.
 
hi annihilannic,

these are the actual error massages:

awk: syntax error near line 9
awk: illegal statement near line 9
awk: syntax error near line 9
awk: illegal statement near line 9
awk: illegal statement near line 10
awk: illegal statement near line 12
awk: illegal statement near line 13
awk: syntax error near line 16
awk: illegal statement near line 16

cheers,tini

 
What operating system are you using? Perhaps try nawk instead of awk if it is available?

Annihilannic.
 
i tried nawk (is available) and got these error massages:

nawk: syntax error at source line 9
context is
for >>> (f<=NF;f++) <<<
nawk: illegal statement at source line 9
nawk: illegal statement at source line 9

cheers, tini
 
o.k....i tried it with the semicolon and i got no error massage, but also no data in the new file. :-(
 
How did you run it? How did you specify the input and output file names?

Annihilannic.
 
i enter the following command:

script input > output
 
Well, if you put it into a script file exactly as I posted, the first parameter input will be ignored. You either need to put the filename in the script file itself, e.g.

Code:
<snip>
                        print a["i_lat",i],a["i_lon",i]
                }
        }
' input

or put in a parameter so that the input filename will be substituted:

Code:
<snip>
                        print a["i_lat",i],a["i_lon",i]
                }
        }
' $1

Annihilannic.
 
hi annihilannic,

the script works very well...thanks a lot!!!!
but there are more then one time i_lon or i_lat in the input file and the script ends after first run. i don't know, where i have to put an end or something like this.

cheers, tini
 
Ah, if you don't state the complete problem, you won't get a complete solution. :)

Code:
nawk '
        BEGIN { max["i_lat"]=0; max["i_lon"]=0 }
        # start from first field
        { f=1 }
        # unless there is an i_, in which case
        # start from third field
        /^i_/ { f=3; x=$1 }
        {
                # add each field to array, increment
                # corresponding counter
                for (;f<=NF;f++) { a[x,max[x]++]=$f }
        }
        END {
                print "i_lat","i_lon"
                for (i=0; i<max["i_lat"]; i++) {
                        print a["i_lat",i],a["i_lon",i]
                }
        }
'

Annihilannic.
 
hi annihilannic,

yes, you are right! my fault!:~/
i have to thank you very much...you helped me very much!

cheers, tini
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top