1

I have stacked trying to make a simple reminder script which can compare the running date with the date that a file has. for example I have a file that contains:

Apr 25 2015 cycling \n
Apr 26 2015 football \n
.\n
.\n
.\n
.\n

i want to compare the running date (reading the file line by line) with the date of the file and if they match then I want to be printed the specific line in my screen. can you help me please? I am trying so hard...

2 Answers 2

5

An easy way would be to use grep:

date +'^%b %d %Y' | grep -f- your_file
3

Using only bash:

#!/bin/bash
today="$(date +"%b %d %Y")"
while IFS= read -r line; do
   [[ $line =~ ^$today ]] && echo "$line"
done </path/to/file.txt
  • The today variable stores that day's formatted date

  • while IFS= read -r line will read each line from file.txt and save it as variable line

  • [[ $line =~ ^$today ]] will test if the line has started with the today's date

  • && echo "$line" will print the line if the previous command returns an exit code 0 i.e. the previous condition is true.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .