ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
#!/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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.