Tutorial :RegExp to match everything up to first blank line



Question:

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,


Solution:1

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  


Solution:2

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


Solution:3

grep -B10000 -m1 ^$ list.txt  
  • -B10000: print 10000 lines before the match
  • -m1: match at most once
  • ^$: match an empty line


Solution:4

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »