4

我正在尝试*.ics使用sed. 该*.ics文件如下所示:

[...]

BEGIN:VEVENT
UID:0xfoo
SUMMARY:foo
DTSTART:20131212T090000
DTEND:20131212T100000
SEQUENCE:0
DTSTAMP:20131212T100000
LOCATION:foo
CATEGORIES:foo
DESCRIPTION:foo
CLASS:PUBLIC
END:VEVENT

[...]

UID我想删除以or开头的行SEQUENCE,但前提是它们介于 BEGIN:VEVENTandEND:VEVENT

我试图删除这些行:

sed '/^BEGIN:VEVENT/,/^END:VEVENT/ /^UID/d'

但它只会返回一个错误,比如unknown command '/'

怎么可能删除这些行?

谢谢!

4

3 回答 3

9

试试这一行:

 sed '/^BEGIN:VEVENT/,/^END:VEVENT/{/^\(UID\|SEQUENCE\)/d}' file
于 2013-10-07T20:06:22.720 回答
5

sed 是在单行上进行简单替换的出色工具,其他任何事情只需使用 awk:

awk '
/BEGIN:VEVENT/ { inBlock = 1 }
inBlock {
    if ( /END:VEVENT/ ) {
        inBlock = 0
    }
    else if ( /^(UID|SEQUENCE)/ ) {
        next
    }
}
{ print }
' file

伪代码解释(inBlock是一个布尔变量,line只是一个虚构的字符串变量):

WHILE read line from file DO

    IF ( line contains the regexp "BEGIN:VEVENT" ) THEN
        inBlock = TRUE

    ENDIF

    IF ( inBlock == TRUE ) THEN

        IF ( line contains the regexp "END:VEVENT" ) THEN
            inBlock = FALSE

        ELSIF ( line starts with the regexp "UID" or "SEQUENCE" ) THEN
            do no further processing on this line

        ENDIF

    ENDIF

    print the current line

ENDWHILE
于 2013-10-07T20:05:31.867 回答
1

其他awk

awk '/^BEGIN:VEVENT/,/^END:VEVENT/ {if ($0~/^UID|^SEQUENCE/) next}1' file

如果行以UIDSEQUENCE在块部分 from BEGIN:VEVENTto开始END:VEVENT,则跳过该行

于 2013-10-08T05:53:22.590 回答