Parsing ICS file with bash

爷,独闯天下 提交于 2019-12-24 00:35:49

问题


This is a google calendar ics file.

I download it each time to check whether new play events have been added or changed and I appear on IRC.

I need convert a file like this :

BEGIN:VEVENT
DTSTART:20160612T201000Z
DTEND:20160612T211000Z
DTSTAMP:20160519T200239Z
UID:xxxxxxxxxxxxxxxxxx@google.com
CREATED:20160518T153226Z
DESCRIPTION:
LAST-MODIFIED:20160518T153226Z
LOCATION:OCS Choc
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Ash vs Evil Dead Saison 1 Episode 9 & 10
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART;TZID=Europe/Brussels:20160611T203500
DTEND;TZID=Europe/Brussels:20160611T233500
DTSTAMP:20160519T202440Z
UID:xxxxxxxx@google.com
RECURRENCE-ID;TZID=Europe/Brussels:20160611T203500
CREATED:20160503T144152Z
DESCRIPTION:
LAST-MODIFIED:20160518T123213Z
LOCATION:RTS Un (Suisse)
SEQUENCE:1
STATUS:CONFIRMED
SUMMARY:The Mysteries Of Laura Saison 2 Episode 1 à 4
TRANSP:TRANSPARENT
END:VEVENT

to

New Events Created :
dim. juin 12  20:10  Ash vs Evil Dead Saison 1 Episode 9 & 10 - OCS Choc

Last Modified Event :
sam. juin 11  20:35  The Mysteries Of Laura Saison 2 Episode 1 à 4 - RTS Un (Suisse)

I need convert with a bash script. I have to get :

DTSTART CREATED LAST-MODIFIED LOCATION SUMMARY

And i need compare CREATED and LAST-MODIFIED

pseudo-code :

if (created = LastModified)
then 
     echo createdevent
else
     echo lastModifiedEvent
fi

回答1:


A native bash implementation (for shell version 4.0 or newer -- older versions lack associative arrays) would look something like the following:

#!/bin/bash

handle_event() {
  : # put a definition of your intended logic here
}

declare -A content=( ) # define an associative array (aka map, aka hash)
declare -A tzid=( )    # another associative array for timezone info

while IFS=: read -r key value; do
  value=${value%$'\r'} # remove DOS newlines
  if [[ $key = END && $value = VEVENT ]]; then
    handle_event # defining this function is up to you; see suggestion below
    content=( )
    tzid=( )
  else
    if [[ $key = *";TZID="* ]]; then
      tzid[${key%%";"*}]=${key##*";TZID="}
    fi
    content[${key%%";"*}]=$value
  fi
done

...where handle_event is a function that does the actual work you care about. For instance, that might look like this:

local_date() {
  local tz=${tzid[$1]}
  local dt=${content[$1]}
  if [[ $dt = *Z ]]; then
    tz=UTC
    dt=${dt%Z}
  fi
  shift

  if [[ $dt = *T* ]]; then
    dt="${dt:0:4}-${dt:4:2}-${dt:6:2}T${dt:9:2}:${dt:11:2}"
  else
    dt="${dt:0:4}-${dt:4:2}-${dt:6:2}"
  fi

  # note that this requires GNU date
  date --date="TZ=\"$tz\" $dt" "$@"
}

handle_event() {
  if [[ "${content[LAST-MODIFIED]}" = "${content[CREATED]}" ]]; then
    echo "New Event Created"
  else
    echo "Modified Event"
  fi
  printf '%s\t' "$(local_date DTSTART)" "${content[SUMMARY]}" "${content[LOCATION]}"; echo
}

With your given input file and the above script, bash parse-ics <test.ics yields the following output (with my current locale, timezone and language):

New Event Created
Sun Jun 12 15:10:00 CDT 2016    Ash vs Evil Dead Saison 1 Episode 9 & 10        OCS Choc
Modified Event
Sat Jun 11 15:35:00 CDT 2016    The Mysteries Of Laura Saison 2 Episode 1 à 4   RTS Un (Suisse)



回答2:


awk is useful for stuff like this. The following can be placed in a new file (ics.awk):

BEGIN{OFS=" "}
$1=="DTSTART"{DTSTART=$2}
$1=="CREATED"{CREATED=$2}
$1=="LAST-MODIFIED"{LASTMODIFIED=$2}
$1=="SUMMARY"{SUMMARY=$2}
$1=="LOCATION"{LOCATION=$2}
$1=="END"{
        if (CREATED==LASTMODIFIED)
                print "\nNew Event Created"
        else
                print "\nLast Modified Event"

        print DTSTART,SUMMARY,LOCATION
}

You can execute it like:

awk -F":" -f ics.awk yourfile.ics

That splits the fields in the file by a colon and the awk script processes the file line by line. It captures values as it finds it, then prints them when it finds a line with "END".

The script above will get you close:

New Event Created
20160612T201000Z Ash vs Evil Dead Saison 1 Episode 9 & 10 OCS Choc

Last Modified Event
20160612T201000Z The Mysteries Of Laura Saison 2 Episode 1 à 4 RTS Un (Suisse)



回答3:


Using the same logic as @JNevill but with associative arrays:

ics.awk

BEGIN { FS=":" }
{ a[$1] = $2 }
$1 == "END" {
  printf("%s\n%s %s %s\n\n", 
    a["CREATED"] == a["LAST-MODIFIED"] ? "New Event Created" : "Last Modified Event", 
    a["DTSTART"], a["SUMMARY"], a["LOCATION"])
} 

And then call it with:

% awk -f ics.awk input-file
New Event Created
20160612T201000Z Ash vs Evil Dead Saison 1 Episode 9 & 10 OCS Choc

Last Modified Event
20160612T201000Z The Mysteries Of Laura Saison 2 Episode 1 Ã  4 RTS Un (Suisse)


Will leave a trailing new line however.



来源:https://stackoverflow.com/questions/37334681/parsing-ics-file-with-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!