1838571 Members
3280 Online
110128 Solutions
New Discussion

Re: Awk script

 
SOLVED
Go to solution
Oktay Tasdemir
Advisor

Awk script

Hello,

I have an awk script which prints out a header, then it prints out the data as specified as a paramater (input_file) and finally it prints out the footer.

My script is performing the header and footer operations correctly however its ommitting the first line in the input file.
Any ideas?

I have attached the input file, I can't attach multiple files so here is the awk script.

## *****************************************************************
if [ $# -ne 1 ]; then
echo "Usage: "
exit 1
fi

infile=$1
date=`date +%C%y%m%d`


awk -F, -v infile=$infile -v date=$date '
BEGIN {
count = 0
}
#
#If header found then create information details
#
{
if (NR == 1)
{
printf("%1s%10s%6s%8s%110s\n\r", "0", "Settlement", "AMPCUS", date, " " )
}
else
{
print $1
count++
}
}
END {
printf("%s%06d%s\r","9", count, " ")
}

' <$infile >$infile.tmp


Cheers
Oktay
Let the fun and games begin
5 REPLIES 5
curt larson_1
Honored Contributor

Re: Awk script

if (NR == 1)
{
printf("%1s%10s%6s%8s%110s\n\r", "0", "Settlement", "AMPCUS", date, " " )
}
else
{
print $1
count++
}

should be

if (NR == 1)
{
printf("%1s%10s%6s%8s%110s\n\r", "0", "Settlement", "AMPCUS", date, " " )
}
print $1;
count++;

and if if was me i'd do it this way

BEGIN {
printf("%1s%10s%6s%8s%110s\n\r", "0", "Settlement", "AMPCUS", date, " " );
}

{
print $1;
count++;
}
END ....
curt larson_1
Honored Contributor

Re: Awk script

if it is the first record your just printing the header. your not printing $1 or incrementing the count for the first record.

or your only printing $1 and incrementing the count if the record number isn't 1.
V.Tamilvanan
Honored Contributor

Re: Awk script

Hi,
Try the below one:

## *****************************************************************
if [ $# -ne 1 ]; then
echo "Usage: "
exit 1
fi

infile=$1
date=`date +%C%y%m%d`


awk -F, -v infile=$infile -v date=$date '
BEGIN {
count = 0
}
#
#If header found then create information details
#
{
if (NR == 1)
{
printf("%1s%10s%6s%8s%110s\n\r", "0", "Settlement", "AMPCUS", date, " " )
}
{
print $1
count++
}
}
END {
printf("%s%06d%s\r","9", count, " ")
}

' <$infile >$infile.tmp
Michael Kelly_5
Valued Contributor
Solution

Re: Awk script

Oktay,
the problem is that the logic of your awk script works like this:
If NR == 1 then printf("%1s...)
There is nothing the the code block to print $1.
When this code block completes, awk reads the next line and NR is no longer equal to 1.

If, for whatever reason, you don't want to put the printf statement in the BEGIN clause (as Curt suggests) then you can do
if (NR == 1)
{
printf("%1s...)
print $1
count++
}
else
{
print $1
count++
}
}
END ...

HTH,
Michael.
The nice thing about computers is that they do exactly what you tell them. The problem with computers is that they do EXACTLY what you tell them.
Oktay Tasdemir
Advisor

Re: Awk script

Thanks for your help guys.
Let the fun and games begin