Finding Data in the file

Hi All

I have a file having columns such as

AAA,BBB,CCC,Aug 13 2013
AAA,BBB,CCC,Aug 11 2013
AAA,BBB,CCC,Aug 12 2013
AAA,BBB,CCC,Aug 13 2013

Now I need to extract the records which are not of todays date (Considering Today date is 13-Aug-2013

So I should get below records

AAA,BBB,CCC,Aug 11 2013
AAA,BBB,CCC,Aug 12 2013

in a file

This is what am doing
Creating a file with only first and forth record via awk

-----------------------------------

cat $FILE_NAME | awk 'BEGIN{FS=",";}
{
#where = match($4,$TODAY_DATE)
#if(match)
print substr($1,1,3),"~",substr($4,1,11);}
END{;}' >> $DATE_IN_FILE

-------------------------------------
Now for comparing date am using the below

TODAY_DATE=`date +"%b %d %Y"`
cat $DATE_IN_FILE | awk 'BEGIN{FS="~";}
{
val=index($2,$TODAY_DATE)

if($val>0)
print "Inside IF"
else

print "Inside else"
;}
END{;}' >> $INCORRECT_RATE

The issue is because the Date is having multiple words Aug 12 2013 and hence Index is not working (Since Aug matches and hence it returns the value)

Please suggest

awk -F, 'BEGIN{today=strftime("%b %e %Y")} !($NF~today)' infile

--ahamed

Ahmed

Could you please explain your code.

strftime is a inbuilt function in awk to print the date in desired format, more or less similar to the date command. This will give the date in MMM DD YYYY format and stores in the variable today . All this is done in the BEGIN block since it is required to be done only once.

Then today is compared with the last field in the file. The delimiter is , , so $NF will yield the last field.

HTH

--ahamed

Thanks a lot Ahamed, appreciate your help.

!($NF~today)
can be change to
$NF!~today
For me, its more easy to test if a field is not something,
compare to test if its equal to something then invert it.
Just a personal opinion :slight_smile:

strftime is not necessarily available in all awk implementations. You could use a parameter (value defined by e.g. the date command) then to supply "today" into awk...

1 Like

The strftime is available, However am Trying to do the below, but not getting the needed result

awk -F, 'BEGIN{today=strftime("%b %e %Y")} 
{ #print substr($NF,1,11)
  #print today
 (substr($NF,1,11)== today)
}
' infile

The reason being that the NF contains the Time as well (Which is always marked as 00:00:00)

First, the input that you showed us in the 1st message in this thread did not contain 00:00:00 in any of the input lines. So, what input are you using now?

Second, you have commented out both print statements in this awk script and the awk statement:

 (substr($NF,1,11)== today)

should not produce any output. So, what is the needed result?

Hi Don Cragun

The feed is same, only the date in the file is appended in the last column along with the date (Which is always 00:00:00).

Regarding Point 2
The lines which I commented was for testing purpose

#print substr($NF,1,11) 

This I used to get exactly what is returned by substr()

 #print today

To check what is the exact value which am getting in today

The Last Line which is actually checking the quality is not commented

 (substr($NF,1,11)== today)

It isn't commented, but it evaluates to 0 or to 1 and prints nothing either way. If what you're trying to do is print lines that have today's date and ignore other lines, change your script to:

awk -F, 'BEGIN{today=strftime("%b %e %Y")} 
substr($NF,1,11) == today
' infile

My awk doesn't include an strftime() function, but the following should also work for you:

awk -F, 'BEGIN{today=strftime("%b %e %Y 00:00:00")} 
$NF == today
' infile