RegExp fits all up to the first blank line

I am writing a bash script that will show me which television programs will be watching today, it will receive this information from a text file.

The text is in the following format:

Monday:
Family Guy (May 2)

Tuesday:
House
The Big Bang Theory (May 3)

Wednesday:
Bill
NCIS
NCIS LA (April 27)

Thursday:
South Park

Friday:
Flashforward

Saturday:

Sunday:
Hignfy
Underbelly

I plan to use "date +% A" to develop the day of the week and use the output in the grep regular expression to return the corresponding lines from my text file.

If someone can help me with a regex that I should use, I will be forever great.

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

+4
source share
4 answers

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 
+2
source
 sed -n '/^Tuesday:/,/^$/p' list.txt 
+2
source
 grep -B10000 -m1 ^$ list.txt 
  • -B10000 : print 10,000 lines before the match
  • -m1 : match not often
  • ^$ : matching empty string
0
source

Alternatively you can use this:

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

This awk script corresponds to a sequential group of lines that starts with / ^ Day: $ / and ends with an empty line. It prints only a line if the line ends with a character that is not a colon. Therefore, it will not print "Sunday:" or an empty line.

0
source

Source: https://habr.com/ru/post/1308560/


All Articles