代码之家  ›  专栏  ›  技术社区  ›  anon

如何删除许多文件中包含单词SID的一行?

  •  -1
  • anon  · 技术社区  · 17 年前

    如何删除许多文件中包含单词SID的一行?

    我想知道在sed和tr之间。然而,他们似乎都没有达到目的。

    这个问题实际上稍微更具挑战性。在某些文件中,我还需要删除匹配后的一行,而在其他文件中,则需要删除匹配前的一行。这一行有一个字符 &

        . "question_sent"
        . "&"                        // I do not want this line
        .  htmlspecialchars(SID)     // NOT wanted
        . "&"
        . "email="
    

    &

        . "successful_registration&"
        . "SID="                    // Not wanted
        .  htmlspecialchars($SID)   // Not wanted
        . "&"                       // not wanted
        . "email="
    

    & &

      if(isset($_GET['ask_question'])) {
          echo  ("<li id='ask_question_active'><a href='?ask_question&amp;"
              .  htmlspecialchars(SID)   // not wanted
              . "&amp;"                 // not wanted
              . "email=
    
    3 回复  |  直到 14 年前
        1
  •  3
  •   too much php    17 年前

    当代码如此不一致时,我不会觉得运行全局搜索和替换是游戏。我会使用grep/vim来检查每一行,除非你真的有10000个更改要做。要使用grep/vim,步骤如下:

    1.

    " <f1> looks for SID in the current file
    map <f1> /\<SID\><CR>
    " <f2> goes to the next file
    map <f2> :next<CR><f1>
    
    " <f5> deletes only the current line, and goes to the next SID
    map <f5> dd
    " <f6> deletes the current line and the one above, and goes to the next SID
    map <f6> k2dd
    " <f7> deletes the current line and the one below, and goes to the next SID
    map <f7> 2dd
    " <f8> deletes the current line, and the one above AND the one below
    map <f8> k3dd
    

    grep

    grep -rl '\bSID\b' * > fix-these-files.txt
    

    您可能需要稍微调整一下,以确保它能找到您需要更改的所有文件。在进入下一步之前,请确保它是正确的。

    vim '+set confirm' '+/\<SID\>' $(cat fix-these-files.txt)
    

    你现在应该已经 vim 在第一个文件中,您需要更改。使用以下步骤修复每次出现的SID:

    • <F5>
    • 如果您需要同时删除上面的行,请按 <F6> <F5> .
    • 如果您需要同时删除下面的行,请按 <F7> <F5>
    • 下面同时按下 <F8> <F5>
    • <F1>
    • <F2>

    全局正则表达式打印 命令从步骤 2.

    .vimrc .

        2
  •  2
  •   Community Mohan Dere    9 年前

    这不能用tr.Sed来完成。可能可以使用,但我对它不够了解,无法举个例子。我会使用perl,然后我可能会引入一些状态变量,参见 this answer single_ampersand_found SID_found


    #!/usr/bin/perl -w
    use strict;
    use warnings;
    
    my $state = 0;
    my $state_ampersand_found = 1;
    my $state_SID_found  = 2;
    
    my $previous_line = "";
    
    while (my $line = <>) {
            chomp($line);
    
            if ($line =~ /"&/) {
                    if ($state == $state_ampersand_found) {
                            print $previous_line;
                    }
                    if ($state == $state_SID_found) {
                            $previous_line = "";
                            $state = 0;
                            next;
                    }
                    $state = $state_ampersand_found;
                    # remember current line, but do not print it (yet)
                    $previous_line = $line . "\n";
                    next;
            }
            if ($line =~ /SID/) {
                    $previous_line = "";
                    $state = $state_SID_found;
                    next;
            }
            $state = 0;
            print $previous_line;
            print $line, "\n";
    }
    
        3
  •  1
  •   Sinan Ünür    17 年前

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    my $re_amp = qr/"&(?:amp;)?"/;
    my $re_sid = qr/SID/;
    
    while ( my $this = <DATA> ) {
        next unless $this =~ /\S/;
    
        if ( $this =~ $re_amp ) {
            $this = skip_while(\*DATA, $re_sid);
        }
        elsif ( $this =~ $re_sid ) {
            $this = skip_while(\*DATA, $re_sid, $re_amp);
        }
    
        print $this if defined $this;
    }
    
    sub skip_while {
        my ($fh, $re1, $re2) = @_;
        my $line;
        while ( $line = <$fh> ) {
            next if (defined $re1 and $line =~ $re1)
                 or (defined $re2 and $line =~ $re2);
            last;
        }
        return $line;
    }
    
    __DATA__
    handlers/handle_new_question.php-        . "question_sent"
    handlers/handle_new_question.php-        . "&"                        // I do not want this line
    handlers/handle_new_question.php:        .  htmlspecialchars(SID)   // NOT wanted
    handlers/handle_new_question.php-        . "&"
    handlers/handle_new_question.php-        . "email="
    
    handlers/handle_registration.php-            . "successful_registration&"
    handlers/handle_registration.php:            . "SID="                   // Not wanted
    handlers/handle_registration.php:            .  htmlspecialchars($SID)   // Not wanted
    handlers/handle_registration.php-            . "&"                  // not wanted
    handlers/handle_registration.php-//            . "email="
    
    views/ask_question_link.php-        if(isset($_GET['ask_question'])) {
    views/ask_question_link.php-            echo  ("<li id='ask_question_active'><a href='?ask_question&amp;"
    views/ask_question_link.php:                .  htmlspecialchars(SID)   // not wanted
    views/ask_question_link.php-                . "&amp;"           // not wanted
    views/ask_question_link.php-//                . "email=
    

    C:\Temp> w
    handlers/handle_new_question.php-        . "question_sent"
    handlers/handle_new_question.php-        . "&"
    handlers/handle_new_question.php-        . "email="
    handlers/handle_registration.php-            . "successful_registration&"
    handlers/handle_registration.php-//            . "email="
    views/ask_question_link.php-        if(isset($_GET['ask_question'])) {
    views/ask_question_link.php-            echo  ("<li id='ask_question_active'><a href='?ask_question&amp;"
    views/ask_question_link.php-//                . "email=