我正在尝试使用awk将sysdir
文件解析为表。
我的ics文件看起来像这样(只在这里发布一个事件):
ics
我只对以BEGIN:VEVENT
DTSTART:20171004T173000Z
DTEND:20171004T183000Z
DTSTAMP:20180209T144026Z
UID:999999c@google.com
CREATED:20171004T171653Z
DESCRIPTION:
LAST-MODIFIED:20171004T173916Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:This text is the summary
TRANSP:OPAQUE
X-APPLE-TRAVEL-ADVISORY-BEHAVIOR:AUTOMATIC
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
X-WR-ALARMUID:282393849382
UID:883928394839283948392
ACKNOWLEDGED:20171004T173915Z
X-APPLE-DEFAULT-ALARM:TRUE
END:VALARM
END:VEVENT
,DTSTART
,DTEND
,CREATED
开头的行感兴趣。
我想出了以下代码
SUMMARY
我正在执行
BEGIN{OFS="\t"}
$1=="DTSTART"{DTSTART=$2}
$1=="DTEND"{DTEND=$2}
$1=="CREATED"{CREATED=$2}
$1=="SUMMARY"{SUMMARY=$2}
{print DTSTART DTEND CREATED SUMMARY}
但似乎某处出现了错误。 另外,打印表头很好。
答案 0 :(得分:4)
只需用
替换最后一行/^END:VEVENT/ {print DTSTART, DTEND, CREATED, SUMMARY}
和第一个
BEGIN{
OFS="\t"
print "DTSTART", "DTEND", "CREATED", "SUMMARY"
}
结果:
DTSTART DTEND CREATED SUMMARY
20171004T173000Z 20171004T183000Z 20171004T171653Z This text is the summary
您正在执行" {打印DTSTART DTEND CREATED SUMMARY}"每一行。
答案 1 :(得分:3)
短 awk
解决方案(针对静态输入文件格式):
awk -F':' '$1~/^(DTSTART|DTEND|CREATED|SUMMARY)/{
printf "%s%s",$2,($1=="SUMMARY"? ORS:"\t")
}' file.ics
输出:
20171004T173000Z 20171004T183000Z 20171004T171653Z This text is the summary