0

I would like to compare line number 13 in two different files. That line contains date in the format: "Nov 8 00:46:57 2021 GMT"

I want to compare these two dates, and check which is less or greater than the other. Can someone please help me with this.

Sai Priya
  • 115
  • 1
  • 4

2 Answers2

1
# Reads line 13 of file1.txt into date1
date1=$(awk 'NR==13' file1.txt)
# Reads line 13 of file2.txt into date2
date2=$(awk 'NR==13' file2.txt)

# converts date to seconds since the Epoch in Linux
date1=$(date -d "$date1" +%s)
date2=$(date -d "$date2" +%s)

if (( date1 >= date2 )); then
  echo "Date in file1.txt is greater or equal"
else
  echo "Date in file2.txt is greater"
fi
user1984
  • 5,990
  • 2
  • 13
  • 32
0

13a and 13b are your file names:

iso8601-13() {
    date -d "`head -13 "$1" | tail -n 1`" --iso-8601=seconds
}

if [[ `iso8601-13 13a` < `iso8601-13 13b` ]] ; then
    echo 13a is earlier
fi
MarJoh
  • 66
  • 4