tags:

views:

114

answers:

4

Hi,

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,

A: 
grep -B10000 -m1 ^$ list.txt
  • -B10000: print 10000 lines before the match
  • -m1: match at most once
  • ^$: match an empty line
KennyTM
Thank you, not the complete solution but it helped.I've now got the following "grep -A10000 Tuesday list.txt | grep -B10000 -m1 ^$"
SKWebDev
This only prints the first day or portion of the file with no ability to select the day or offset.
drewk
A: 

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
+1  A: 

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
drewk
A: 

sed -n '/^Tuesday:/,/^$/p' list.txt

At Santos