Quick awk tip :)

how do i "awk" the date after the from only to compare it on a if statement later .

filename example:

server1-ips-ultranoob-ok_From_2012_21_12-23:40:23_To_2012_21_12-23:49:45.zip

what i want o do is compare only the date from the string in "From_2012_21_12" in this case i only want the "2012_21_22"

and make the if comparison but i got that cover :slight_smile:

Tnx guys .:b:

filename="server1-ips-ultranoob-ok_From_2012_21_12-23:40:23_To_2012_21_12-23:49:45.zip"
DT=$( echo $filename | awk -F"From_" ' { print substr($2,1,10) } ' )
echo $DT
2012_21_12
2 Likes

well i must be doing something wrong ... lets see :

today_date =$(date +"%Y"_"%m"_"%d") # this returns ex: 2012_11_21

for i in  $(ls)
do
    if [ $(echo $i | awk -F"From_" ' { print substr($2,1,10) } ' ) eq $today_date]
    then
        mv $i $today_date
    else
       echo $i >> not_moved.txt
   fi
done

i try to put in the if echo "$i" == "$today_date" i get unary operator

Corrections:-

today_date=$(date +"%Y"_"%m"_"%d") # this returns ex: 2012_11_21

for i in * # OR you can use *.zip
do
        if [ $( echo "$i" | awk -F"From_" ' { print substr($2,1,10) } ' ) = "$today_date" ]
        then
                mv "$i" "$today_date"
        else
                echo "$i" >> not_moved.txt
        fi
done

today_date = is wrong.
today_date= is correct.