- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Awk script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 10:55 PM
06-10-2003 10:55 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2003 11:57 PM
06-10-2003 11:57 PM
Re: Awk script
{
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 ....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2003 12:01 AM
06-11-2003 12:01 AM
Re: Awk script
or your only printing $1 and incrementing the count if the record number isn't 1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2003 12:04 AM
06-11-2003 12:04 AM
Re: Awk script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2003 04:17 AM
06-11-2003 04:17 AM
Solutionthe 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-11-2003 11:45 PM
06-11-2003 11:45 PM