Home > Blockchain >  Bash script to compare a particular line in two different files, and compare dates [duplicate]
Bash script to compare a particular line in two different files, and compare dates [duplicate]

Time:09-21

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.

CodePudding user response:

# 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

CodePudding user response:

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
  • Related