LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 07-28-2009, 07:40 AM   #1
moos3
LQ Newbie
 
Registered: Mar 2007
Posts: 4

Rep: Reputation: 0
Perl find file and then replace string in file


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
 
Old 07-28-2009, 08:37 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
use sed

sed -i 's/foo/bar/' <filename>
 
Old 07-28-2009, 08:40 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
perl -pi.bak -e 's/THIS/THAT/g' files

man find2perl
 
Old 07-28-2009, 11:45 AM   #4
moos3
LQ Newbie
 
Registered: Mar 2007
Posts: 4

Original Poster
Rep: Reputation: 0
can I call sed like that in a perl script?
 
Old 07-28-2009, 03:39 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.
 
Old 07-29-2009, 07:10 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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]
 
  


Reply

Tags
perl



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 02:25 PM
how to find and replace only the 2nd occurrence of similar string in a file hchoonbeng Linux - Newbie 1 10-08-2008 03:44 AM
how to replace string by other one in a file & without perl ? Xeratul Programming 4 05-25-2007 11:13 PM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 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