LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-11-2008, 11:48 AM   #1
centosfan
Member
 
Registered: Jun 2003
Location: Golem city
Distribution: Server - Debian Desktop - Linux Mint
Posts: 219

Rep: Reputation: 32
Replacing text in multiple files ?


Is that possible on linux?I have two or three lines which i need to change and it is contained in 240 files which is a lot of time requied to do with manual changing.First what i need to delete line which include cgi file,and second one is path which i need to change to proper path.

So how do i do that ?
Line which i want to delete is
<?php virtual('/cgi-bin/atx/in.cgi');?>

Last edited by centosfan; 09-11-2008 at 11:51 AM.
 
Old 09-11-2008, 12:08 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Code:
> cat testfile2
line 1
<?php virtual('/cgi-bin/atx/in.cgi');?>
line3
line4
jschiwal@hpmedia:~/test> sed -f deleteline.sed -i testfile2
jschiwal@hpmedia:~/test> cat testfile2
line 1
line3
line4
jschiwal@hpmedia:~/test> cat deleteline.sed
/<?php virtual('\/cgi-bin\/atx\/in.cgi');?>/d
The -i option lets you edit the files in situ and will operate on each file instead of cat'ting the file arguments. You could use wildcard(s) instead of testfile2 or search for the lines in grep and pipe the filelist to the sed command using xargs.

Last edited by jschiwal; 09-11-2008 at 12:12 PM.
 
Old 09-11-2008, 12:17 PM   #3
centosfan
Member
 
Registered: Jun 2003
Location: Golem city
Distribution: Server - Debian Desktop - Linux Mint
Posts: 219

Original Poster
Rep: Reputation: 32
Ok i will try that but i also finded one solution which works but it seems it requires to put slash before each character so i am little confused.
Here is working code:
perl -pi -w -e 's/BABA//g;' *.php
This replace all text called BABA to nothing.If i put
perl -pi -w -e 's/BABA/RABA/g;' *.php
all BABA text is replaced to RABA.
But it seems if you have special characters then you need to put slash,and that make entire thing little complicated.I finded that at this url:
http://www.liamdelahunty.com/tips/li...iple_files.php
I tried this one:
perl -pi -w -e 's/\<\?php\virtual\(\'\/cgi\-bin\/atx\/in\.cgi\'\)\;\?\>//g;' *.php
But it seems i missed something since it doesnt work.
Update:I finded what causing problem,it is this '
For some reason that blocks execution of command,probaly beacuse it have other meaning in that command.It seems i will have to find another solution for replacing text everytime when i encounter '
Update2:actualy i finded solution by simply replacing ' with " and it worked.This is how code should look:
perl -pi -w -e "s/\<\?php virtual\('\/cgi\-bin\/atx\/in\.cgi'\)\;\?\>//g;" *.php

Last edited by centosfan; 09-11-2008 at 01:02 PM.
 
Old 09-11-2008, 01:28 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
My solution assumes that the line is by itself and deletes the line. Your perl example is similar to using sed:
sed 's/BABA//' filename

The reason for escaping those characters is because many of the characters in the line you gave are interpreted by the shell before it gets to the perl or sed command. I cheated a bit and wrote the sed command in a sed program. I still needed to escape the forward slash however.

If my assumption was wrong, then putting:
s#<?php virtual('/cgi-bin/atx/in.cgi');?>##
in a sed program will replace the matching string with nothing. To be more exacting, the . in "in.cgi" should be escaped as well. The "." is a single character wild card.
s#<?php virtual('/cgi-bin/atx/in\.cgi');?>##

Now if this is on a line by itself, there will be a blank line left over. Your perl example uses the same search/replace operation. The syntax is just a bit different. Sed is about 1/5 the size of perl, but if the rest of the script is a perl script, then use the perl example inside your script.

Your real decision is whether to learn perl or learn sed or both. The -i option does make using sed to process multiple files easier.
 
Old 09-15-2008, 10:58 AM   #5
centosfan
Member
 
Registered: Jun 2003
Location: Golem city
Distribution: Server - Debian Desktop - Linux Mint
Posts: 219

Original Poster
Rep: Reputation: 32
I used perl example and it works fine,just need to be careful with " and '
But now i encountered another problem,how to apply command to files in subfolders.
I tried adding -r or r to perl -pi -w -e 's/bla\/output/bla\/raba\_raba/g;' *.shtml
command but it doesnt work.Temporal solution will be to make batch command and putting it into usr bin so i can call it in each directory where i need to apply this search replace command but it will be better if i could simply type command only one time.
 
Old 09-17-2008, 12:24 PM   #6
centosfan
Member
 
Registered: Jun 2003
Location: Golem city
Distribution: Server - Debian Desktop - Linux Mint
Posts: 219

Original Poster
Rep: Reputation: 32
I guess there is no way to apply search & replace command to subdirectory files...
 
Old 09-17-2008, 08:55 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Use a loop of the type

Code:
for file in `find <path> -name <pattern> ...`
do
    edit file (eg solns as above)
done
 
Old 09-19-2008, 04:48 PM   #8
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 483

Rep: Reputation: 153Reputation: 153
There are many ways to do this. Instead of find you could try grep -Rl as in grep -Rl target-string parent-directory. The -R option searches directories recursively while the -l option simply lists the files that match the target string.

Alternatively you could do all of this in perl. Perl has a powerful built in find function which can search recursively for files with a certain extension and which match a certain string. You would then call a function which performs the actual text processing on that subset of files. You wouldn't even need a loop. A single recursive find call with appropriate filters would do the trick. What you were trying on the command line won't work but you could do anything you want in the actual script.
 
Old 09-20-2008, 06:43 AM   #9
centosfan
Member
 
Registered: Jun 2003
Location: Golem city
Distribution: Server - Debian Desktop - Linux Mint
Posts: 219

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by tofino_surfer View Post
There are many ways to do this. Instead of find you could try grep -Rl as in grep -Rl target-string parent-directory. The -R option searches directories recursively while the -l option simply lists the files that match the target string.

Alternatively you could do all of this in perl. Perl has a powerful built in find function which can search recursively for files with a certain extension and which match a certain string. You would then call a function which performs the actual text processing on that subset of files. You wouldn't even need a loop. A single recursive find call with appropriate filters would do the trick. What you were trying on the command line won't work but you could do anything you want in the actual script.
Well as you can see default command is
perl -pi -w -e 's/bla\/output/bla\/raba\_raba/g;' *.shtml
I am sure there is requied one more letter or something else to make it work but dont know what.
 
Old 09-20-2008, 07:08 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by chrism01 View Post
Use a loop of the type

Code:
for file in `find <path> -name <pattern> ...`
do
    edit file (eg solns as above)
done
a while read loop would be better
Code:
find .... | while read ..
do
  ...
done
 
Old 09-20-2008, 07:25 AM   #11
centosfan
Member
 
Registered: Jun 2003
Location: Golem city
Distribution: Server - Debian Desktop - Linux Mint
Posts: 219

Original Poster
Rep: Reputation: 32
............

Last edited by centosfan; 09-20-2008 at 09:57 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
How to Insert text into multiple files mpetrovic Linux - Software 5 01-15-2010 10:56 PM
Batch Text Extract Multiple Files lixbie Programming 10 07-02-2008 09:56 AM
Steps needed to convert multiple text files into one master text file jamtech Programming 5 10-07-2007 11:24 PM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 08:16 PM
How to replace text in multiple files bpk Linux - Newbie 2 02-10-2004 02:03 PM

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

All times are GMT -5. The time now is 07:01 PM.

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