Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

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

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

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.

Examples of the data

Here &

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

No & here

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

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=
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) 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) This grep command will find all the files you need to change:

grep -rl 'SID' * > 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) Use vim to open all the files that need fixing, like this:

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

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:

  • 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.

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

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

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...