1752767 Members
4990 Online
108789 Solutions
New Discussion юеВ

Re: awk query

 
SOLVED
Go to solution
himacs
Super Advisor

awk query

Hi Admins,

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

8 REPLIES 8
James R. Ferguson
Acclaimed Contributor

Re: awk query

Hi:

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...
himacs
Super Advisor

Re: awk query

Hi JRF,

Thanks for the response.

Can we print previous date using awk..

Regards
himacs
himacs
Super Advisor

Re: awk query

Hi ,

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
James R. Ferguson
Acclaimed Contributor

Re: awk query

HI (again):

> 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...
himacs
Super Advisor

Re: awk query

Hi JRF,

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
James R. Ferguson
Acclaimed Contributor

Re: awk query

Hi (again):

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...
himacs
Super Advisor

Re: awk query

Hi JRF,

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


James R. Ferguson
Acclaimed Contributor
Solution

Re: awk query

Hi:

> 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...