4

I'm writing a bash script that will show me what TV programs to watch today, it will get this information from a text file.

The text is in the following format:

Monday:
Family Guy (2nd May)

Tuesday:
House
The Big Bang Theory (3rd May)

Wednesday:
The Bill
NCIS
NCIS LA (27th April)

Thursday:
South Park

Friday:
FlashForward

Saturday:

Sunday:
HIGNFY
Underbelly

I'm planning to use 'date +%A' to work out the day of the week and use the output in a grep regex to return the appropriate lines from my text file.

If someone can help me with the regex I should be using I would be eternally great full.

Incidentally, this bash script will be used in a Conky dock so if anyone knows of a better way to achieve this then I'd like to hear about it,

SKWebDev
  • 161
  • 1
  • 5

4 Answers4

2

Perl solution:

#!/usr/bin/perl 

my $today=`date +%A`; 
$today=~s/^\s*(\w*)\s*(?:$|\Z)/$1/gsm;

my $tv=join('',(<DATA>));

for my $t (qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday)) {
    print "$1\n" if $tv=~/($t:.*?)(?:^$|\Z)/sm; 
}   

print "Today, $1\n" if $tv=~/($today:.*?)(?:^$|\Z)/sm; 

__DATA__
Monday:
Family Guy (2nd May)

Tuesday:
House
The Big Bang Theory (3rd May)

Wednesday:
The Bill
NCIS
NCIS LA (27th April)

Thursday:
South Park

Friday:
FlashForward

Saturday:

Sunday:
HIGNFY
Underbelly
dawg
  • 98,345
  • 23
  • 131
  • 206
2
sed -n '/^Tuesday:/,/^$/p' list.txt
Cosmin
  • 21,216
  • 5
  • 45
  • 60
At Santos
  • 21
  • 1
0
grep -B10000 -m1 ^$ list.txt
  • -B10000: print 10000 lines before the match
  • -m1: match at most once
  • ^$: match an empty line
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Thank you, not the complete solution but it helped. I've now got the following "grep -A10000 Tuesday list.txt | grep -B10000 -m1 ^$" – SKWebDev May 01 '10 at 21:32
  • This only prints the first day or portion of the file with no ability to select the day or offset. – dawg May 02 '10 at 16:11
0

Alternatively, you can use this:

awk '/^'`date +%A`':$/,/^$/ {if ($0~/[^:]$/) print $0}' guide.txt

This awk script matches a consecutive group of lines which starts with /^Day:$/ and ends with a blank line. It only prints a line if the line ends with a character that is not a colon. So it won't print "Sunday:" or the blank line.

tiftik
  • 978
  • 5
  • 10