perl instead of grep 'conversion' help?

Hello:

I butchered a shell script together, but my script kung-fu is 5 years old and all I've used for 5 years are for i loops. I hope that some perl guru can do in 5 minutes what takes me 5 weeks, so I am asking for help.

I have a simple script using "grep -A 2 'string' /path/to/file" but after importing my data into it (if's a FF plugin called ReminderFox), the rows are "re-ordered"

script now outputs: (source ics file is 'US Holidays')

  1. SUMMARY:Washington's Birthday
    DTSTART;VALUE=DATE:20090216
    DTEND;VALUE=DATE:20090217

But grep'ing with the same command on my backup file, I get this kind of output:

  1. SUMMARY:Technical Support Specialist
    UID:1281092384117-979283602
    DTSTART:20100812T085900''

In simplest terms, my request is...
I'd like to have these fields from any ics file that has them:
SUMMARY:
DTSTART;VALUE=DATE:
DTEND;VALUE=DATE:
(and echo a newline)

and if possible convert DTSTART|DTEND data...
from it's current format of "+%Y%m%d" to "+%h %m %Y"

This is NOT homework.
Bash = GNU bash, version 4.0.38 | Awk = GNU Awk 3.1.7
Grep = GNU grep 2.6.3 | perl = 5.10.0

I hope this is clear. :slight_smile:
Thank you for your time.

I will assume you meant "+%d %m %Y" and not "+%h %m %Y".

$ 
$ 
$ perl -lne '/SUMMARY/ && print;
             /(DTSTART;VALUE=DATE:)(\d{4})(\d\d)(\d\d)/ && print "$1$4 $3 $2";
             /(DTEND;VALUE=DATE:)(\d{4})(\d\d)(\d\d)/ && print "$1$4 $3 $2\n"
            ' basic.ics
DTSTART;VALUE=DATE:16 02 2009
DTEND;VALUE=DATE:17 02 2009

SUMMARY:Washington's Birthday
DTSTART;VALUE=DATE:15 02 2010
DTEND;VALUE=DATE:16 02 2010

SUMMARY:Washington's Birthday
DTSTART;VALUE=DATE:11 11 2009
DTEND;VALUE=DATE:12 11 2009

SUMMARY:Veterans Day
DTSTART;VALUE=DATE:11 11 2010
DTEND;VALUE=DATE:12 11 2010

...
... <output snipped for brevity>
...

SUMMARY:Christmas
DTSTART;VALUE=DATE:01 04 2010
DTEND;VALUE=DATE:02 04 2010

SUMMARY:April Fool's Day
DTSTART;VALUE=DATE:01 04 2009
DTEND;VALUE=DATE:02 04 2009

SUMMARY:April Fool's Day
$ 
$ 
$ 

tyler_durden

tyler:

Flawless! it works great.
Yes, I meant date "+%h %m %Y"
I like my stuff 'human readable" (Sep 09 2010 vs 12 09 2010)

n/m I just swapped out
"$1$4 $3 $2" with
"$1$3 $4 $2"

How can I save that into a .pl file, and run it later?
and how about only showing the current year?

Thank you for your time, sir. I greatly appreciate it.

How to save it into a .pl file ?
Well, open up your favorite text editor, type in the Perl program, save and quit so that "cat -n basic.pl" spews something like the following:

$ 
$ 
$ cat -n basic.pl
     1    #!/usr/bin/perl -w
     2    # determine the current year first
     3    my $year = (localtime)[5]+1900;
     4    # set the "ics" file
     5    my $file = "basic.ics";
     6    # process the file now
     7    open (F, $file) or die "Can't open $file: $!";
     8    while (<F>) {
     9      if (/SUMMARY/) {
    10        print;
    11      } elsif (/(DTSTART;VALUE=DATE:)($year)(\d\d)(\d\d)/) {
    12        print "$1$3 $4 $2\n";
    13      } elsif (/(DTEND;VALUE=DATE:)($year)(\d\d)(\d\d)/) {
    14        print "$1$3 $4 $2\n\n"
    15      }
    16    }
    17    close (F) or die "Can't close $file: $!";
$ 
$ perl basic.pl
SUMMARY:Washington's Birthday
DTSTART;VALUE=DATE:02 15 2010
DTEND;VALUE=DATE:02 16 2010

SUMMARY:Washington's Birthday
SUMMARY:Veterans Day
DTSTART;VALUE=DATE:11 11 2010
DTEND;VALUE=DATE:11 12 2010

...
... <output snipped for brevity>
...

SUMMARY:Christmas Eve
SUMMARY:Christmas Eve
DTSTART;VALUE=DATE:12 25 2010
DTEND;VALUE=DATE:12 26 2010

SUMMARY:Christmas
SUMMARY:Christmas
DTSTART;VALUE=DATE:04 01 2010
DTEND;VALUE=DATE:04 02 2010

SUMMARY:April Fool's Day
SUMMARY:April Fool's Day
$ 
$ 

The program takes into account the current year requirement as well.

tyler_durden

By awk, and GNU date.

awk '
BEGIN{FS=OFS=":"}
/^SUMMARY/{print}
/DTSTART;VALUE=/||/DTEND;VALUE=/{"date -d "$NF" \"+%h %m %Y\" "|getline $NF;print}
' infile


SUMMARY:Washington's Birthday
DTSTART;VALUE=DATE:Sep 09 2010
DTEND;VALUE=DATE:Sep 09 2010