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
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
#!/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
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