Suppose there are two files in web/ named foo.php and bar.php. The 1st line of foo.php is "sdajgeSTRINGdsad" and the 10th line of bar.php is "gdfhu98324STRING". The task is to first locate these two files, then delete 1st line of foo.php and 10th line of bar.php given that the specified string is "STRING".
|
|
||||
|
|
|
|
|||||||||||||||
|
|
To find a file containing a string, use grep -R "STRING" web/ To edit a file and remove lines containing a string, there are many options but here are two common ones. Since you want to operate on entire lines this is really easy :) First you could use
For your second example we'll do the same thing with
The second way would be to use
Pretty much the same thing could be accomplished by copying the file yourself then using sed to filter the backup and over-write the original:
Now to combine these two tasks! Let's make a function so you can do this repeatedly with different strings:
Once you create that function, you could use it like this:
Any files containing STRING in web/ would then have those lines removed and a backup copy would be made of the original version. You can create a function like this in your current shell or you can write it to your |
|||||
|
|
There are two alternatives:
|
|||||
|
|
Just make use of
|
|||||
|
