LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-21-2010, 10:40 PM   #1
admod
LQ Newbie
 
Registered: Sep 2010
Posts: 18

Rep: Reputation: 0
Question How to delete a line with certain format from files in a directory


Hi Friends,
I need a PHP script to delete a line with certain pattern from all files
in a directory. The Directory contain files with extensions .js,.html and
.php. Do any body give a working code snippet to Read all files in a
directory with above extension and delete that line from the files.
I am waiting for your quick reply.

Regards
Anes P.A
 
Old 09-22-2010, 12:07 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
This is best done in a language such as python, perl, or bash shell. You will want to look into: regular expressions, conditional statements, for loops, read line, open/close files, read/write files, python file arguments. Those are the python topics you should look up.

We will not write your code for you but we will help you along the way. If you already have a script written but need to trouble shoot it then post your source code and we will assist you with finding a solution.

Go ahead and try to write a script based on the topics I've stated and we'll work from there as a team.

Once you have the script you're looking for then you can use the shell to recursively list the files which you would need to write your commands.

Code:
for x in `find . -name "*\.js" -type f`;do /path/to/python/script/myscript.py $x;done
for x in `find . -name "*\.htm*" -type f`;do /path/to/python/script/myscript.py $x;done
for x in `find . -name "*\.php" -type f`;do /path/to/python/script/myscript.py $x;done
You can also do it all using for loops in the shell with grep which would allow your files to be modified using only the terminal. Just research bash shell programing, how to pipe commands, shell regular expressions, grep man page, find man page, xargs man page, sed man page, sed regex. This way you can skip python and php scripts altogether.

That should be more than enough information to get you started.

Last edited by sag47; 09-22-2010 at 12:20 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can not delete files via Nautilus, only via command line leupi Linux - General 3 01-30-2013 02:33 AM
Delete files in a directory - RM MDeFreitas Linux - Newbie 3 06-25-2010 07:00 AM
Delete Files in directory villumanati Linux - General 7 12-30-2008 01:18 PM
rm can't delete 999999 files in one directory yelo Linux - Software 10 04-10-2004 02:00 PM
How can I delete a directory and its files? OrganicX Linux - General 6 09-01-2002 04:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration