awk sub() issue in Unix

Hi, I am new to unix shell script and I have some trouble on the awk sub

I would like to pick the Date "July 10 2012" into $corr_date by using sub() function, but it is not successful.

The inputted text file:
pic.*.txt
July 10 2012 20:30:50 , 1234567.jpg
July 10 2012 20:30:52 , 5648978.jpg
July 10 2012 20:30:53 , 6405789.jpg

#!/bin/ksh

curr_date=`date +"%B %d %Y"`

for i in `ls -1 pic.*.txt`
  do
     Date=`tail -1 $i |awk 'BEGIN {FS=","} {print $1}'`
     corr_date=`awk '{sub(/[0-9]+:[0-9]+:[0-9]+/,"") ; print;}' Date`

     if [ "$corr_date" = "$curr_date"]; then
                echo "$corr_date is valid."
     else
                echo "$corr_date is invalid"
     fi
  done

  exit

The error msg is:
awk: syntax error near line 1
awk: illegal statement near line 1

Thanks

I think you can simplify things a bit by using a single awk to suss out the last record of the file in $i and assign it to corr_date:

corr_date=$(  awk 'END { print $1, $2, $3; }' $i )

Your code would become:


#!/bin/ksh

curr_date=`date +"%B %d %Y"`

for i in `ls -1 pic.*.txt`
  do
     corr_date=$(  awk 'END { print $1, $2, $3; }' $i )
     if [ "$corr_date" = "$curr_date"]; then
                echo "$corr_date is valid."
     else
                echo "$corr_date is invalid"
     fi
  done
1 Like

Thank for your reply but I try to run this script and discover that "corr_date" do not have any value inside.

try running the script in debug mode to find more. :wink:

I have use debug mode already, it does not display anything :frowning:

space needed in the below line

 
if [ "$corr_date" = "$curr_date" ]; 

Are you getting any error ?

Yes, the corr_date's value is empty

make sure, your last line in the file is not empty

It can be further improved like this:

for i in pic.*.txt

instead of for i in `ls -1 pic.*.txt` . That is UUOBM1L (useless use of back ticks, minus 1 and ls ;))

yes, i am sure that the txt file is not empty in the last line.
My expected output is
July 10 2012 is valid.

but the actual output is that " is invalid"

can you post your script

after the calculation, just print it and see, what it comes

 
corr_date=$(  awk 'END { print $1, $2, $3; }' $i )
echo $corr_date

Try temporarily using

#!/bin/ksh -x

on line 1 to see what is going on..

My Script:

#!/bin/ksh -x

curr_date=`date +"%B %d %Y"`

for i in pic.*.txt
  do
     corr_date=$(  awk 'END { print $1, $2, $3; }' $i )
     if [ "$corr_date" = "$curr_date" ]; then
                echo "$corr_date is valid."
     else
                echo "$corr_date is invalid"
     fi
  done

+ ./pack.ksh
+ + date +%B %d %Y
curr_date=July 11 2012
+ + awk END { print $1, $2, $3; } pic.01.txt
corr_date=
+ [ = July 11 2012 ]
+ echo is invalid
is invalid

check the last line in this file -- pic.01.txt

checked. The last line is July 10 2012 20:30:53 , 6405789.jpg
Do not have the empty line.
I edit something here

#!/bin/ksh -x

curr_date=`date +"%B %d %Y"`

for i in pic.*.txt
  do
     Date=`tail -1 $i |awk 'BEGIN {FS=","} {print $1}'`
     corr_date=`awk 'END {print $1, $2, $3;}' $Date`
     if [ "$corr_date" = "$curr_date" ]; then
                echo "$corr_date is valid."
     else
                echo "$corr_date is invalid"
     fi
  done

And it shows:

+ date +%B %d %Y
curr_date=July 11 2012
+ + awk BEGIN {FS=","} {print $1}
+ tail -1 pic.01.txt
Date=July 10 2012 20:30:53
+ + awk END {print $1, $2, $3;} July 10 2012 20:30:53
awk: can't open July
corr_date=
+ [ = July 11 2012 ]
+ echo is invalid
is invalid

change the below line

 
corr_date=`awk 'END {print $1, $2, $3;}' $Date`
to
corr_date=`echo $Date | awk 'END {print $1, $2, $3;}'`
1 Like

Nice, Thanks itkamaraj
You give me a hint to solve the problem!
My final script is to split the string again:

#!/bin/ksh -x

curr_date=`date +"%B%d%Y"`

for i in pic.*.txt
  do
     Date=`tail -1 $i |awk 'BEGIN {FS=","} {print $1}'`
     corr_date=`echo $Date | awk 'BEGIN {FS=" "} {print $1 $2 $3;}'`

     if [ "$corr_date" = "$curr_date" ]; then
                echo "$corr_date is valid."
     else
                echo "$corr_date is invalid"
     fi
  done