Find the answer to your Linux question:
Results 1 to 3 of 3
I was wondering if there was a way to search file content and remove the line that was found. I have thousands of php scripts that need lines removed and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    4

    search file content and remove that line?


    I was wondering if there was a way to search file content and remove the line that was found.
    I have thousands of php scripts that need lines removed and i don't want to do this one by one.

  2. #2
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    682
    Google is your friend.

    http://www.linuxforums.org/forum/lin...tml#post577367

    You should be able to do something suitable with that, although remember not to redirect the output directly back into your php file or it will get clobbered.

    Let us know how you get on,

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  3. #3
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    If you have thousands of files to edit I would use the sed command with the -i option (edit files in place) within the find command. Something like this:

    Code:
    find /path/to/files -maxdepth 1 -type f -name "*.php" -exec sed -i '/pattern/ d' {} \;
    'pattern' is the line to delete. If you specify the entire line then prepend and append it with '^' (start of line) and '$' (end of line) to make sure sed deletes only that line. Make sure to copy/backup the files before you run the command just in case something goes wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •