LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags

Reply
 
Thread Tools
Old 07-28-2009, 08:40 AM   #1
moos3
LQ Newbie
 
Registered: Mar 2007
Posts: 4
Thanked: 0
Perl find file and then replace string in file


[Log in to get rid of this advertisement]
I have script that I'm working on that updates a username in all the files that are called blah.inc for my framework. since i host a bunch of these web apps i need to do it to all of them. so I need to figure out how to update these files automagically with out me watching it to call vim every time. heres what I have so far
Code:
#!/usr/bin/perl
#
# search for a file in all subdirectories
#
if ($#ARGV != 0){
        print "usage: Find filename\n";
        exit;
}
$filename = $ARGV[0];

# look in current directory
$dir = `pwd`;
chop($dir);
&searchDirectory($dir);

sub searchDirectory {
    local($dir);
    local(@lines);
    local($line);
    local($file);
    local($subdir);

    $dir = $_[0];

    # check for permission
    if(-x $dir) {

        # search this directory
        @lines = `cd $dir; ls -l | grep $filename`;
        foreach $line (@lines) {
            $line =~ m/\s+(\S+)$/is;
            $file = $1;
            if ($file eq 'config.inc'){
                print "Found $file in $dir\n";
            }
        }

        # search any sub directories
        @lines = `cd $dir; ls -l`;
        foreach $line (@lines) {
            if($line =~ /^d/) {
                $line =~ /\s+(\S+)$/;
                $subdir = $dir."/".$1;
                &searchDirectory($subdir);
            }
        }
    }
}
This finds the files but now i need to figure out how to do s/bob/fred/g on those files
moos3 is offline  
Tag This Post
Reply With Quote
Old 07-28-2009, 09:37 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 165
Thanked: 21
use sed

sed -i 's/foo/bar/' <filename>
PMP is offline     Reply With Quote
Old 07-28-2009, 09:40 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,710
Thanked: 19
perl -pi.bak -e 's/THIS/THAT/g' files

man find2perl
bigearsbilly is offline     Reply With Quote
Old 07-28-2009, 12:45 PM   #4
moos3
LQ Newbie
 
Registered: Mar 2007
Posts: 4
Thanked: 0

Original Poster
can I call sed like that in a perl script?
moos3 is offline     Reply With Quote
Old 07-28-2009, 04:39 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 1,034
Thanked: 50
To OP: your script is full of global variables. Why ?

And no, you do not need to call 'sed' from a Perl script - Perl has the 's' operator.

In order to perform search and replace operation in file you need to open the files - source for reading, destiantion - for writing. Since you are a newbie, do not even try to edit files in-place. It's a bad idea almost always.

You can, however, edit a file in memory.
Sergei Steshenko is offline     Reply With Quote
Old 07-29-2009, 08:10 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,710
Thanked: 19
Quote:
Originally Posted by moos3 View Post
can I call sed like that in a perl script?
I showed you the perl equivalent!


or if you want it in a script.

Code:
#!/usr/bin/perl

local $^I = ".bak"; # in place editing with backup file

@ARGV = qw(file1 file2 file3); # set this to your list of files
while (<>) {
   s/THIS/THAT/g;
}
if you don't want a backup use:
Code:
#!/usr/bin/perl -i

@ARGV = qw(file1 file2 file3);
while (<>) {
   s/THIS/THAT/g;
}
simplesss

your find script is bad. why are you calling grep from perl??????
use File::Find or glob

e.g:

Code:
@ARGV = (<*.txt>,<*.doc>);
[/code]
bigearsbilly is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux command to find and replace string within text file chips11 Linux - Newbie 5 11-24-2008 03:25 PM
how to find and replace only the 2nd occurrence of similar string in a file hchoonbeng Linux - Newbie 1 10-08-2008 04:44 AM
how to replace string by other one in a file & without perl ? Xeratul Programming 4 05-26-2007 12:13 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 07:02 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 04:07 AM


All times are GMT -5. The time now is 03:40 PM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration