1

I want to compare dates in a file in shell script with the below format with the current date and return some flag.

 Sep 15 2017 11:04AM

 Sep 16 2017 10:07AM

 Sep 19 2017 11:44AM

 Sep 20 2017 10:08AM

 Sep 21 2017 10:03AM

Awk scripting can be used here.

BlackBeard
  • 10,246
  • 7
  • 52
  • 62
ninja67
  • 25
  • 2
  • 6
  • Hello, what kind of comparison you want to make and what is the result you want, what flags do you want to be risen. – Gustavo Magalhães Oct 10 '17 at 05:49
  • i have a file with some data out if which there is a field with the date in format above. i want to compare the current date with the date in the file and redirect the line containing the date which is older than 7 days into a new file. – ninja67 Oct 10 '17 at 05:58
  • `to compare dates in a file` Can you show us how that file looks like ? – Hakan Baba Oct 10 '17 at 06:04
  • You can use this, https://stackoverflow.com/questions/1401482/yyyy-mm-dd-format-date-in-shell-script#1401495answer, to read the formated date and then make a simple comparison. Unfortunately I can test now to see if it works. – Gustavo Magalhães Oct 10 '17 at 06:06

1 Answers1

0

Assuming the file that contains the dates looks like this:

$ cat file1
Sep 15 2017 11:04AM
Sep 16 2017 10:07AM
Sep 19 2017 11:44AM
Sep 20 2017 10:08AM
Sep 21 2017 10:03AM
Oct 02 2017 10:03AM
Oct 05 2017 10:03AM
Oct 07 2017 10:03AM

Use this for GNU date

cat file1 | xargs -I@ bash -c " \
    seven_days_ago=$(date --date='7 days ago' +%s); \
    input=\$(date --date=\"@\" +%s); \
     if [ \"\$seven_days_ago\" -gt \"\$input\" ]; then \
        echo \"@\"; \
     fi;"

And this for BSD date

cat file1 | xargs -I@ bash -c " \
    seven_days_ago=$(date -v-7d +%s); \
    input=\$(date -j -f \"%b %d %Y %R%p\" \"@\" +%s); \
     if [ \"\$seven_days_ago\" -gt \"\$input\" ]; then \
        echo \"@\"; \
     fi;"

Output is the following. This dropped the date October 05 and 07 which are closer than 7 days.

Sep 15 2017 11:04AM
Sep 16 2017 10:07AM
Sep 19 2017 11:44AM
Sep 20 2017 10:08AM
Sep 21 2017 10:03AM
Oct 02 2017 10:03AM

How do you differentiate between GNU date and BSD date?

To check execute:

$ man date  | tail -n 1
GNU coreutils 8.22 

vs

$ man date | tail -n 1
BSD                             August 16, 2007                            BSD
Hakan Baba
  • 1,897
  • 4
  • 21
  • 37
  • The file contains details like below : `code` asdffsdvdsds,sdffsfdswe,123142121,adf123,12341411sdcscr,Sep 15 2017 11:04AM asdffsdvdsds,sdffsfdswe,123142121,adf123,12341411sdcscr,Sep 15 2017 11:04AM asdffsdvdsds,sdffsfdswe,123142121,adf123,12341411sdcscr,Sep 15 2017 11:04AM `code` Also the current code gives the below error : date: invalid date bash: line 0: [: : integer expression expected – ninja67 Oct 10 '17 at 09:46
  • @ninja67 Did you check what `date` command you are running ? Execute `man date | tail -n 1` – Hakan Baba Oct 10 '17 at 15:43
  • @ninja67 I have extended my answer to handle both date commands. Take a look and let me know if you still have problems. – Hakan Baba Oct 10 '17 at 17:14
  • the code is capturing the complete line and not taking the last field which is the date we have to use for comparison. Hence getting the error as invalid date. Also the date is GNU . – ninja67 Oct 11 '17 at 07:25
  • @ninja67 of course the command I have written would not work with the file contents you have shown in the comment here. At the start of my answer I have showed my assumption how the input file looks like. In order to modify your input file you need to execute this `cat file1 | awk -F "," '{print $6}'` . That will remove everything except the last column of the date string. – Hakan Baba Oct 11 '17 at 19:55
  • how can we redirect the results to a different file? – ninja67 Oct 12 '17 at 06:38
  • @ninja67 Take a look here https://stackoverflow.com/questions/637827/redirect-stderr-and-stdout-in-bash – Hakan Baba Oct 12 '17 at 06:55
  • I tried that as below : if [ \"\$seven_days_ago\" -gt \"\$input\" ]; then \ echo \"@\" >>file; \ fi;" – ninja67 Oct 12 '17 at 08:33