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&"
. htmlspecialchars(SID) // not wanted
. "&" // not wanted
. "email=
Updated again: I think this fixes the bug in the previous script I posted.
Output:
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:
2) This
grep
command will find all the files you need to change: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:
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:<F5>
.<F6>
instead of<F5>
.<F7>
instead of<F5>
<F8>
instead of<F5>
<F1>
to find an another occurrence of SID to fix.<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!
This cannot be done with tr. Sed can probably be used but I do not know it well enough to give an example. What I would have used is perl, and then I would probably introduced some state variables, see this answer for an example of what I mean. Probably I would use states
single_ampersand_found
(do not print the line yet, if next line contains SID, forget it else print it) andSID_found
(if next line contains & forget that line).Update: the following code will suppress all the lines now marked with "not wanted" plus the fourth line in your first example (i.e a bug), but I figure it should be good enough for you to correct and adapt to your needs.