搜索和删除匹配模式前后的行
Seach and delete lines before and after a matching a pattern
输入包含由 { 和 } 框起来的日历条目,
我想删除所有 Mozilla 创建的条目,如下所示,并将结果保存在一个新文件中。
{
"c_content" = "BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
BEGIN:VTIMEZONE
..
END:VCALENDAR";
"c_name" = "0000000-0000-000-0000-00000000000.ics";
},
一个日历条目的内容总计为 39 行。
gawk 'BEGIN{RS=ORS="};"}/Mozilla.org\/NONSGM/;END{printf "\n"}' inputfile
这会过滤掉所有匹配的条目,但我会得到相反的结果(比如 grep -v)。
Input sample:
包含该行的第二个日历条目
"PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN"
应该过滤掉。
{
"c_content" = "BEGIN:VCALENDAR
PRODID:-//Inverse inc./SOGo 5.1.0//EN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Europe/Berlin
LAST-MODIFIED:20210303T135712Z
X-LIC-LOCATION:Europe/Berlin
BEGIN:DAYLIGHT
TZNAME:CEST
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20180518T085937Z
LAST-MODIFIED:20180518T090431Z
DTSTAMP:20180518T090432Z
UID:005A3BF6-39A9-4771-8170-FD9E74AA818B
SUMMARY:Firma Byom
DTSTART;TZID=Europe/Berlin:20180518T130000
DTEND;TZID=Europe/Berlin:20180518T140000
CLASS:PUBLIC
DESCRIPTION:Prospekt \U00DCbergabe
SEQUENCE:1
TRANSP:OPAQUE
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
END:VALARM
END:VEVENT
END:VCALENDAR";
"c_name" = "005A3BF6-39A9-4771-8170-FD9E74AA818B.ics";
},
{
"c_content" = "BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:W. Europe Standard Time
BEGIN:STANDARD
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU;BYHOUR=3;BYMINUTE=0
TZNAME:Mitteleurop\U00E4ische Zeit
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU;BYHOUR=2;BYMINUTE=0
TZNAME:Mitteleurop\U00E4ische Sommerzeit
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
CLASS:PUBLIC
DTEND;VALUE=DATE:20220330
DTSTAMP:20220406T184433Z
DTSTART;VALUE=DATE:20220329
PRIORITY:5
SEQUENCE:0
SUMMARY:This entry has to be filtered out
TRANSP:TRANSPARENT
UID:040000008200E00074C5B7101A82E008000000000015970CF649D801000000000000000
0100000001EAC086F1BE63E448C32EE561BCB4A1E
X-MICROSOFT-CDO-BUSYSTATUS:FREE
END:VEVENT
END:VCALENDAR";
"c_name" = "040000008200E00074C5B7101A82E008000000000015970CF649D8010000000000000000100000001EAC086F1BE63E448C32EE561BCB4A1E.ics";
},
{
"c_content" = "BEGIN:VCALENDAR
PRODID:-//Inverse inc./SOGo 5.1.0//EN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Europe/Berlin
LAST-MODIFIED:20210303T135712Z
X-LIC-LOCATION:Europe/Berlin
BEGIN:DAYLIGHT
TZNAME:CEST
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20200124T121710Z
LAST-MODIFIED:20200310T145851Z
DTSTAMP:20200310T145853Z
UID:0085F167-6A6E-4D8D-BE15-F1CAEF2C6CD0
SUMMARY:Herr Mayer
DTSTART;TZID=Europe/Berlin:20200127T083000
DTEND;TZID=Europe/Berlin:20200127T093000
CLASS:PUBLIC
DESCRIPTION:Besuch
SEQUENCE:0
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR";
"c_name" = "0085F167-6A6E-4D8D-BE15-F1CAEF2C6CD0.ics";
},
建议 awk
个脚本:
脚本 1,基于计数线。假设每条记录长 36 行
awk '{--skip}/Mozilla.org/{skip=36}skip<1{print}' input.txt
关心第一个记录 2 行头部,最后一个记录 34 行尾部。
脚本 2,假设记录边距为 },
。假设每条记录以 },
结尾
awk '!/Mozilla.org/{printf "%s",[=11=] RT}' RS="[[:space:]]*}," input.txt
注意最后一条记录的尾部。是否以},
结尾
非常感谢!遗憾的是,由于记录并非都是 36 行长,因此我尝试了脚本 2。我没有得到任何输出。当我省略“!”我确实得到了 Mozilla.org 条目,所以它似乎与“!”相反没有效果。
输入包含由 { 和 } 框起来的日历条目, 我想删除所有 Mozilla 创建的条目,如下所示,并将结果保存在一个新文件中。
{
"c_content" = "BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
BEGIN:VTIMEZONE
..
END:VCALENDAR";
"c_name" = "0000000-0000-000-0000-00000000000.ics";
},
一个日历条目的内容总计为 39 行。
gawk 'BEGIN{RS=ORS="};"}/Mozilla.org\/NONSGM/;END{printf "\n"}' inputfile
这会过滤掉所有匹配的条目,但我会得到相反的结果(比如 grep -v)。
Input sample:
包含该行的第二个日历条目 "PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN" 应该过滤掉。
{
"c_content" = "BEGIN:VCALENDAR
PRODID:-//Inverse inc./SOGo 5.1.0//EN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Europe/Berlin
LAST-MODIFIED:20210303T135712Z
X-LIC-LOCATION:Europe/Berlin
BEGIN:DAYLIGHT
TZNAME:CEST
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20180518T085937Z
LAST-MODIFIED:20180518T090431Z
DTSTAMP:20180518T090432Z
UID:005A3BF6-39A9-4771-8170-FD9E74AA818B
SUMMARY:Firma Byom
DTSTART;TZID=Europe/Berlin:20180518T130000
DTEND;TZID=Europe/Berlin:20180518T140000
CLASS:PUBLIC
DESCRIPTION:Prospekt \U00DCbergabe
SEQUENCE:1
TRANSP:OPAQUE
BEGIN:VALARM
ACTION:NONE
TRIGGER;VALUE=DATE-TIME:19760401T005545Z
END:VALARM
END:VEVENT
END:VCALENDAR";
"c_name" = "005A3BF6-39A9-4771-8170-FD9E74AA818B.ics";
},
{
"c_content" = "BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:W. Europe Standard Time
BEGIN:STANDARD
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU;BYHOUR=3;BYMINUTE=0
TZNAME:Mitteleurop\U00E4ische Zeit
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU;BYHOUR=2;BYMINUTE=0
TZNAME:Mitteleurop\U00E4ische Sommerzeit
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
CLASS:PUBLIC
DTEND;VALUE=DATE:20220330
DTSTAMP:20220406T184433Z
DTSTART;VALUE=DATE:20220329
PRIORITY:5
SEQUENCE:0
SUMMARY:This entry has to be filtered out
TRANSP:TRANSPARENT
UID:040000008200E00074C5B7101A82E008000000000015970CF649D801000000000000000
0100000001EAC086F1BE63E448C32EE561BCB4A1E
X-MICROSOFT-CDO-BUSYSTATUS:FREE
END:VEVENT
END:VCALENDAR";
"c_name" = "040000008200E00074C5B7101A82E008000000000015970CF649D8010000000000000000100000001EAC086F1BE63E448C32EE561BCB4A1E.ics";
},
{
"c_content" = "BEGIN:VCALENDAR
PRODID:-//Inverse inc./SOGo 5.1.0//EN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Europe/Berlin
LAST-MODIFIED:20210303T135712Z
X-LIC-LOCATION:Europe/Berlin
BEGIN:DAYLIGHT
TZNAME:CEST
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
DTSTART:19700329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
DTSTART:19701025T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
CREATED:20200124T121710Z
LAST-MODIFIED:20200310T145851Z
DTSTAMP:20200310T145853Z
UID:0085F167-6A6E-4D8D-BE15-F1CAEF2C6CD0
SUMMARY:Herr Mayer
DTSTART;TZID=Europe/Berlin:20200127T083000
DTEND;TZID=Europe/Berlin:20200127T093000
CLASS:PUBLIC
DESCRIPTION:Besuch
SEQUENCE:0
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR";
"c_name" = "0085F167-6A6E-4D8D-BE15-F1CAEF2C6CD0.ics";
},
建议 awk
个脚本:
脚本 1,基于计数线。假设每条记录长 36 行
awk '{--skip}/Mozilla.org/{skip=36}skip<1{print}' input.txt
关心第一个记录 2 行头部,最后一个记录 34 行尾部。
脚本 2,假设记录边距为 },
。假设每条记录以 },
结尾
awk '!/Mozilla.org/{printf "%s",[=11=] RT}' RS="[[:space:]]*}," input.txt
注意最后一条记录的尾部。是否以},
非常感谢!遗憾的是,由于记录并非都是 36 行长,因此我尝试了脚本 2。我没有得到任何输出。当我省略“!”我确实得到了 Mozilla.org 条目,所以它似乎与“!”相反没有效果。