如何删除许多文件中包含单词 SID 的一行? [英] How can you remove one line which contains the word SID in many files?

查看:70
本文介绍了如何删除许多文件中包含单词 SID 的一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can you remove one line which contains the word SID in many files?

我在 sed 和 tr 之间犹豫.然而,它们似乎都没有达到这个目的.由于以下原因,我也希望有一些灵活性.

I wondering between sed and tr. However, neither of them seem to work for the purpose. I also would like to have some flexibility because of the following reason.

这个问题实际上更具挑战性.我需要在某些文件中的匹配后删除一行,而在其他文件中删除一行.一行具有字符 & ,它决定是删除下方或上方的一行还是仅删除匹配项.最简单的方法可能是列出不同类型的文件,然后使用不同的代码删除每个列表中的文件.

The problem is actually slightly more challenging. I need to remove also one line after the match in some files, while one line before in the other files. The one line has the character & which determines whether to remove one line below or above or only the match. It is likely that the easiest way is to make a list of different types of files and then remove the files in each list with different codes.

这里 &

    . "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="

字符 & 现在采用 HTML 编码,即 &

The character & is now in HTML encoding that is &

  if(isset($_GET['ask_question'])) {
      echo  ("<li id='ask_question_active'><a href='?ask_question&amp;"
          .  htmlspecialchars(SID)   // not wanted
          . "&amp;"                 // not wanted
          . "email=

推荐答案

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

I wouldn't feel game to run a global search-and-replace when the code is so inconsistent. I would be using grep/vim to check each line, unless you seriously do have 10,000 changes to make. To use grep/vim, the steps would be something like this:

1) 将以下内容添加到您的 .vimrc:

1) Add the following to your .vimrc:

" <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

2) 这个 grep 命令会找到你需要修改的所有文件:

2) This grep command will find all the files you need to change:

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

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

You may need to tweak it slightly to make sure that it is finding all the files you need to change. Make sure it is correct before you go to the next step.

3) 使用vim打开所有需要修复的文件,如下:

3) Use vim to open all the files that need fixing, like this:

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

4) 您现在应该打开 vim,并查看您需要更改的第一个文件中的第一个 SID.使用以下步骤修复每次出现的 SID:

4) You should now have vim open, and looking at the first SID in the first file you need to change. Use the following steps to fix each occurrence of SID:

  • 如果您只需要删除当前行,请按.
  • 如果需要同时删除上一行,按而不是.
  • 如果需要同时删除下面一行,按而不是
  • 如果您需要同时删除上面的行,请按而不是<代码>
  • 查找另一个要修复的 SID.
  • 当在当前文件中找不到 SID 时,按 转到下一个文件.立>
  • If you only need to delete the current line, press <F5>.
  • If you need to delete the line above at the same time, press <F6> instead of <F5>.
  • If you need to delete the line below at the same time, press <F7> instead of <F5>
  • If you need to delete the lines above and below at the same time, press <F8> instead of <F5>
  • Press <F1> to find an another occurrence of SID to fix.
  • When SID cannot be found in the current file any more, press <F2> to go to the next file.

当没有更多 SID 需要修复时退出 vim.

Exit vim when there are no more SIDs to be fixed.

5) 再次运行步骤 (2) 中的 grep 命令,检查以确保您获得所有内容.应该没有搜索匹配.

5) Check to make sure you got everything, by running the grep command from step (2) again. There should be no search matches.

6) 删除您在步骤 (1) 中添加到 .vimrc 的额外映射.

6) Delete the extra mappings you added to your .vimrc in step (1).

警告:我还没有测试过上述步骤,如果您使用它们,请注意您只进行了您需要的更改!

Warning: I haven't tested the steps above, if you use them, be careful that you only make exactly the changes you need!

这篇关于如何删除许多文件中包含单词 SID 的一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆