- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk query
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
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
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-07-2011 04:11 AM
тАО06-07-2011 04:11 AM
Please explain about NF and NR variables with awk.
As per my understanding NF stands for last field of the file.
#bdf|awk '{print $NR}'
Filesystem
2097152
341768
2294632
67%
/tmp
Please explain the output of above code.
Regards
himacs
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2011 04:22 AM
тАО06-07-2011 04:22 AM
Re: awk query
In 'awk' the builtin variable 'NF' is the number of fields in a record (line) based on splitting the record into fields based on the record separator (by default, whitespace).
The 'NR' variable is the number of the record (line) where a record is defined by the record separator (by default, a newline).
In the example you show, you asked 'awk' to print the *field* represented by the *record number* --- an odd, and probably wrong choice.
If you wanted the mount point (the last field) you would have written:
# bdf|awk '{print $NF}'
To skip the header line from 'bdf' you could do:
# bdf|awk 'NR>1 {print $NF}'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2011 04:29 AM
тАО06-07-2011 04:29 AM
Re: awk query
Thanks for the response.
Can we print previous date using awk..
Regards
himacs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2011 04:44 AM
тАО06-07-2011 04:44 AM
Re: awk query
Ok,i can print current date as below
#date +%d-%m-%Y
07-06-2011
But how to print previous date 06-06-2011.
Regards/himacs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2011 04:54 AM
тАО06-07-2011 04:54 AM
Re: awk query
> Can we print previous date using awk..
The HP-UX supplied 'awk' lacks some of the nice enhancements of GNU 'awk'. In either, you can call an external command ('date' for example) and in the GNU version you have 'systime()' and 'strftime' (the former to return Epoch seconds; the later to format timestamps).
If I understand you question, correctly, though, you need to use Perl.
I'm coping to copy-and-paste two responses I offered to another member in an earlier thread:
You might note that by using Perl and the 'strftime' function you can choose any date format you want. The format directives are equivalent to those you are familiar with in the 'date' command. Of course, see too the manpages for 'strftime(3C)'. For example:
Find the date 2-days ago from today:
# perl -MPOSIX -le 'print strftime "%d %b",localtime(time-(60*60*24*2))'
05 Jun
# perl -MPOSIX -le 'print strftime "%m/%d/%Y",localtime(time-(60*60*24*2))'
06/05/2011
You could also use this to find a date n-days from a specific starting date:
# cat ./fromwhen
#!/usr/bin/perl
use strict;
use warnings;
use POSIX qw(strftime);
use Time::Local;
my $date = shift or die "Date (YYYYMMDD) expected\n";
die "YYYMMDD expected\n" unless
my ( $yyyy, $mm, $dd ) = ( $date =~ /(\d{4})(\d\d)(\d\d)/ );
my $when = shift or die "Number of days in past expected\n";
my $secs = timelocal(0, 0, 0, $dd, $mm-1,$yyyy );
print strftime "%Y/%m/%d\n",localtime($secs-(60*60*24*$when));
1;
# ./fromwhen 20110607 7
2011/05/31
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2011 04:59 AM
тАО06-07-2011 04:59 AM
Re: awk query
Thanks for the nice reply.But m not familiar with perl.
#date +%d" "%m" "%Y|awk '{print '$1=$1-1',$2,$3}'
sh: 1: Parameter not set.
As expected ended up with error.Is there any option to replace '$1=$1-1' field..
Regards
himacs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2011 05:04 AM
тАО06-07-2011 05:04 AM
Re: awk query
I should clarify that with GNU 'awk' you could with little effort compute a timestamp n-days (or hours, etc.) before or after the current time in Epoch seconds with 'systime'. Then, simply use 'strftime' to format it into human-readable values. This would offer you something akin to the Perl script I offered. You can install GNU 'awk' from here:
http://hpux.connect.org.uk/hppd/hpux/Gnu/gawk-3.1.8/
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2011 05:08 AM
тАО06-07-2011 05:08 AM
Re: awk query
Below code prints previous date.
#date +%d" "%m" "%Y
07 06 2011
#date +%d" "%m" "%Y|awk '$1=$1-1'
6 06 2011
Regards
himacs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2011 05:10 AM
тАО06-07-2011 05:10 AM
Solution> date +%d" "%m" "%Y|awk '{print '$1=$1-1',$2,$3}'
...should have been:
# date "+%d %m %Y"|awk '{print ($1-1),$2,$3}'
6 06 2011
...but you are sliding down a slippery slope. What happens when the day of the month is one (1)? Your code fails.
Regards!
...JRF...