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!

AWK syntax error

Status
Not open for further replies.

yfm

Technical User
Feb 15, 2012
8
UA
I'm a newbie to AWK and now i need to write a script that would extract some numerical values from the source file and print them in an appropriate way to an output file. AWK reports a syntax error concerning operator "if" and { -like gaps. I don't know, what is the problem, everything seems to be ok. Please give me some piece of advice. Thank you!
Here is an abstract from the script code:

BEGIN {
flag=1
flagef=1
}
#
flagef==1 {
if ($1=="basis") {
efline=NR+10
flagef=2
}
}
flagef==2 && NR==efline {
ef=$2
flagef=0
}
flag==1 {
if ($1=="basis") {
line=NR+3
flaga=1
flag=0
}
}
flaga==1 && NR==line {
print (%2/1000,".0") $5
flaga=2
line2=NR+1
}
 
I guess the problem is here:
print (%2/1000,".0") $5

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
here %2 depicts the variable, which was earlier introduced in the batch file and refers to filename (it takes values of 0, 3000, 6000 and so on up to 60000) the point is to print for example 3.0 for 3000 and so on, and then after a spce to print the fifth field...
AWK marks all the gaps like errors
 
Hi

yfm said:
here %2 depicts the variable, which was earlier introduced in the batch file
Huh ? Do you mean DOS batch-like parameter ? Anyway, some details on the used environment, Awk implementation and command line could help us understand what are you trying to do.


Feherke.
 
Hi

Awk will not receive such things automagically. Set it as variable from the command line :
Code:
awk [highlight]-vwhatever=%2[/highlight] -f line.awk
( Note that I am not sure whether you have to write %2 or other number there. )
Code:
  flaga[teal]==[/teal][purple]1[/purple] [teal]&&[/teal] [blue]NR[/blue][teal]==[/teal]line [teal]{[/teal]
        [COLOR=chocolate]print[/color] [highlight]whatever[/highlight][teal]/[/teal][purple]1000[/purple] [green][i]".0"[/i][/green][teal],[/teal][navy]$5[/navy]
        flaga[teal]=[/teal][purple]2[/purple]
        line[purple]2[/purple][teal]=[/teal][blue]NR[/blue][teal]+[/teal][purple]1[/purple]
  [teal]}[/teal]


Feherke.
 
Thanks a bunch! Could you recommend me some faqs or something like that about batch syntax? I ain't sure about it...
And another question - I've tried to execute line.awk for one file directly from cmd and the result was wrong - it only printed 2 values in a column, while there should be more than two values and in a row.
Does awk print those values in a row or in a column by default if the file is written like my one? How can i force it to print values in a row?
 
Once again, if you provide some sample input data and the corresponding output you expect for that data it would make it much easier for us to a) understand your requirements and b) test a solution, if necessary.

print always appends a new line. Try printf "%d.0 %s ",whatever/1000,$5 instead perhaps, and at the end of your script add an END { print "" } section to make it terminate the line when it's done.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Thanks. The sample input file is attached below. All of them have the same structure. The name is also the same except the number (0 in the attached file, it changes as it's said above - from 0 to 60 000 with the step of 3 000)
The output file should contain 21 lines of the same structure. Each line is extracted from the single input file (1st line - from 439GT-0-res.sx, 2nd - from 439GT-3000-res.sx and so on)
The line of the output file should have the next structure(the numbers of lines are given counting from the very first line of the file):
{field #5 of line#28} {#5 of #29} {(#6+#7) of 28} {(#6+#7) of 29} {#8 of #28} {#8 of #29} {(#9 of #28)*(#2 of #35)*1.6E-07} {(#9 of #29)*(#2 of #35)*1.6E-07} {#3 of #32}
Entries in the line should be separated with a single space
 
 http://www.mediafire.com/?ddfk0ec5duddecc
At the moment the batch doesn't work - it only creates a file named "--rf.tmp" that contains a path to the folder, where it is executed and the code of the nfeat.bat - "awk -vbr=%2 -f line.awk".
 
Annihilannic said:
print always appends a new line. Try printf "%d.0 %s ",whatever/1000,$5 instead perhaps, and at the end of your script add an END { print "" } section to make it terminate the line when it's done

Could you tell me about that in a more detailed way? how do "%d" and "%s" work?
 
The reason you only get one line of output is because you only print something when flaga==1. flaga is only set to 1 in the section which runs when flag==1, and that section only runs once because you set flag to 1 at the beginning of the script, then you change it to 0... and then never change it to 1 again.

printf is based on the C function for formatted printing. %d and %s are part of the format string which is described in detail here. %d is a placeholder for an integer, and %s for a string.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Thanks! How do you think, are these flags necessary? or i could just reassign NR to different lines and build the condition only on it?
 
No, I don't think the flags are necessary; I would do something like this:

Code:
[blue]$1[/blue]==1 && [blue]NF[/blue]==11 { [b]split[/b]([blue]$0[/blue],hom_g1) }
[blue]$1[/blue]==2 && [blue]NF[/blue]==11 { [b]split[/b]([blue]$0[/blue],hom_g2) }
[blue]$1[/blue]==1 && [blue]NF[/blue]==4 { [b]split[/b]([blue]$0[/blue],scat_g1) }
[green]/^Ef=/[/green] { [b]printf[/b] [red]"[/red][purple]%f %f %f ... %f\n[/purple][red]"[/red],hom_g1[5],hom_g2[5],hom_g1[6]+hom_g1[7],scat_g1[3] }

Obviously you will need to replace the "..." with a few more "%f" placeholders and add the rest of the numbers you want to include/calculate.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top