0

let say, the date appearing in text file at 4th position in DD-Mmm-YYYY format, I need to update the date T with T+2. if the day false on Thu or Fri then date should be replaced with T+4. ignore weekends.

I tried a script but my if condition is not working

#!/bin/sh
if [ $# -ne 1 ]
then echo "Usage: read.sh filename";
exit 1;
fi

while read line
 do
  echo $line
 olddate=$(echo $line | cut -d, -f4)
echo "olddate $olddate"
echo $(date -d $olddate +%a)
if [ "date -d $olddate +%a" = Thu ] || [ "date -d $olddate +%a" = Fri ]; then
newdate=$(date +%d-%b-%Y -d "$olddate 4 day")
else
newdate=$(date +%d-%b-%Y -d "$olddate 2 day")
fi
echo "newdate $newdate"
record=$(echo $line | sed "s|${olddate}|${newdate}|")
echo "new record $record"
done < $1
  • 1
    Please edit your question to include sample data. Both original and transformed values. – roaima Jan 10 '18 at 16:47
  • 1
  • Define 'not working'. How is it not working as you expect it to? What have you done to attempt to mitigate this? – DopeGhoti Jan 10 '18 at 16:59
  • Feed your code into shellcheck.net you'll get a number of errors that you need to fix. Including your if condition issue. – roaima Jan 10 '18 at 17:10
0

For these two comparisons, you have given two strings which will always compare false, so they need to be replaced with a command substitution:

if [ "date -d $olddate +%a" = Thu ] || [ "date -d $olddate +%a" = Fri ]; then
    newdate=$(date +%d-%b-%Y -d "$olddate 4 day")
else
    newdate=$(date +%d-%b-%Y -d "$olddate 2 day")
fi

ie

today="$(date -d $olddate +%a)"
if [ "$today" = Thu ] || [ "$today" = Fri ]; then
    newdate=$(date +%d-%b-%Y -d "$olddate 4 day")
else
    newdate=$(date +%d-%b-%Y -d "$olddate 2 day")
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.