开发者

RegExp to match everything up to first blank line

开发者 https://www.devze.com 2022-12-28 16:11 出处:网络
I\'m writing a bash script that wi开发者_Python百科ll show me what TV programs to watch today, it will get this information from a text file.

I'm writing a bash script that wi开发者_Python百科ll 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,


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


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消