本文介绍了用bash解析ICS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Google日历ics文件.

This is a google calendar ics file.

我每次都下载它以检查是否已添加或更改了新的播放事件,并且出现在IRC上.

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:[email protected]
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:[email protected]
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

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)

我需要使用bash脚本进行转换.我必须得到:

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

DTSTART已建立最后修改地点总结

DTSTARTCREATEDLAST-MODIFIEDLOCATIONSUMMARY

我需要比较CREATED和LAST-MODIFIED

And i need compare CREATED and LAST-MODIFIED

伪代码:

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

推荐答案

本机bash实现(对于Shell版本4.0或更高版本-旧版本缺少关联数组)将类似于以下内容:

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

...其中handle_event是执行您关心的实际工作的函数.例如,可能看起来像这样:

...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
}

使用给定的输入文件和上面的脚本,bash parse-ics <test.ics产生以下输出(使用我当前的语言环境,时区和语言):

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)

这篇关于用bash解析ICS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 10:22