Do While Loop Problem.

Hello,

My below text is not working....from cat keyword onwards. Any idea why?
Is there any syntax problem while fetching the Fd and Fn?

DATE=`date`
 
cat test.txt | while read line 
do
Fd=`echo $line | awk '{print ($6),($7)}'`
Fn=`echo $line | awk '{print $9}'`
if [ "$Fd" = "$DATE" ] || [ "$Fd" -lt "$DATE" ]; then
gzip $Fn
else 
exit
fi
done

Wat is the format of the test.txt file?
Does your system have GNU date (try "date --date today")?

Hi,

I am actually using "date --date 1 month ago", which is fine.
Issue is not here. it seems.

The format is also tab limited, which is find to pick for awk command.

Yes, it's text, but what is the text? :smiley:

Also, what shell are you using? I don't think any shell but KSH can compare dates, and even then I'm not sure it can do so in that manner... If you can get your outputs and comparisons in epoch seconds("date +%s" for GNU date) that should work nicely just with plain numeric comparisons.

You've got a useless use of cat in there, and a useless use of awk. awk is a powerful enough language that you could probably write this entire script in it: Running two seperate instances per line is not a good idea. Just using shell builtins is hundreds of times faster for small amounts of data. Rule of thumb is, don't run anything to process a single line, builtins only!

DATE=`date`
 
while read A1 A2 A3 A4 A5 A6 A7 A8 Fn A10
do
        Fd="${A6} ${A7}"
        # "-lt" is for integers-only, afaik
        if [[ "${Fd}" == "$DATE" ]] || [[ "${Fd}" -lt "$DATE" ]]
        then
                gzip "$Fn"
                continue
        fi

        # Do you really want it to quit every time a file isn't too old?
        exit
done < test.txt

Thanks. :slight_smile:
One more help on this with if condition:

I need to put,

if [ "${Fd}" = "1month before" ) || [ "${Fd}" -lt "1 month before"]
Then
gzip "$Fn"
else
exit
.........
Now all the dates above are coming in this format: "Nov 30"
I am also able to get 1 month before date in the same format: "Oct 30"
BUT HOW I WILL COMPARE THESE FILE DATE (Fd) and 1month BEFORE DATE IN 'IF' STATEMENT?
HOW MONTHS WILL GET COMPARE?

Use +%s format with date to convert your dates to seconds past epoch. Then it's a simple numeric comparison

eg:

MONTH_AGO=$(date --date -1month +%s)
while read A1 A2 A3 A4 A5 A6 A7 A8 Fn A10
do
        Fd=$(date --date "${A6} ${A7}" +%s)
        if [ $Fd -le $MONTH_AGO ]
        then
                gzip "$Fn"
                continue
        fi
 
        # Do you really want it to quit every time a file isn't too old?
        exit
done < test.txt

Thanks :slight_smile: :slight_smile:
All done.