nested BEGIN-END blocks in awk
awk 'BEGIN {print FILE}
{printf "Part\t\tStart\t\tEnd"
printf "%s %-6s %-6d",Part1,1,$end1
printf "%s %-6d %-6d",Part2,$start2,$end2}
END {printf "total records:%d", $tot}' >meta.txt
This is my code and is not working. It is a part of a shell script, all the variables are computed in other part of script. My I/P files are tables, and I have to find out no. of records in them and dispay the results in a file 开发者_如何学Goin the form of a table like
File Start End
File1 1 205
file2 206 417
and so on
If those are shell variables, you need to pass them into the AWK script:
awk -v "start2=$start2" -v "end1=$end1" -v "end2=$end2" -v "tot=$tot" 'BEGIN {print FILE}
{printf "Part\t\tStart\t\tEnd"
printf "%s %-6s %-6d", Part1, 1, end1
printf "%s %-6d %-6d", Part2, start2, end2}
END {printf "total records:%d", tot}' >meta.txt
There is only one BEGIN and END block in awk. I actually don't really understanding what you are trying to do. So I expect you to show more examples. however, to create a file table in awk, you can do it in the BEGIN block
awk 'BEGIN{
print "field1\tfield2\n"
...
print "one\ttwo"
...
}' > outputfile.txt
if you have an input file to process, and the table contents comes from this input file
awk 'BEGIN{
print "field1\tfield2\n"
}
{
...
print "one\ttwo"
...
}END{
print "so some final stuff here"
}' inputfile > outputfile.txt
精彩评论