Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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 wrote this perl script a few years ago that does this recursively.
Code:
#!/usr/bin/perl
#####
# I haven't figured out why, but '@' symbol must be escaped in $find and NOT
# escaped in $replace
#####
# The string we are looking for
$find = '../white.jpg';
# The string we are replacing it with
$replace = '35468/jpg/white.jpg';
# add any particular file type to search for
# (e.g. @extensions = ( "html", "shtml", "pl");)
@extensions = ( "html" );
foreach $extension (@extensions) {
# Search the filesystem for all specified file types
open (FILELIST, "/usr/bin/find . -name \\\*.$extension|");
while (<FILELIST>) {
$file = $_;
# skip over the "notthisdir" pages (we already know we're moving)
if ($file =~ m/notthisdir/) {
next;
};
# we need to escape certain characters in file names
# (and probably find out who used them and give 'em a beating)
$file =~ s/ /\\ /g;
$file =~ s/\&/\\\&/g;
$file =~ s/\$/\\\$/g;
$file =~ s/~/\\~/g;
$file =~ s/\(/\\\(/g;
$file =~ s/\)/\\\)/g;
# let's lose the newline at the end of the file name
chomp ($file);
# if we have an empty file name skip it
if ($file eq '') {
next
};
# Rather than read and write each file, make sure the file has
# the search term first
open (GREP, "/usr/bin/grep -l $find $file|");
while (<GREP>) {
$skipthis = $_;
}
close (GREP);
if ($skipthis eq '') {
next;
} else {
print "Updating file: $skipthis;
$skipthis = '';
}
# Read in the page or die
open(PAGE,"$file") || die "I can't open $file";
while(<PAGE>){
$thisPage= $thisPage . $_;
}
close(PAGE);
# do our string substitutions
$thisPage =~ s#$find#$replace#g;
# write out the same page with chages
open(PAGENEW,">$file") || die "I can't write the new html to $file";
print PAGENEW $thisPage;
close(PAGENEW);
# reset some values
$thisPage="";
$file="";
}
close (FILELIST);
}
Any ideas why it might not work? Is it because of the "/"s in the pathnames?
Hmmm... odd. I tested this on some dummy files I made that contained a number of paths (including your example path to replace) as well as some other text, and after running the command I checked the files and the content had indeed been changed. So I'm a bit confused :|
Perhaps if you ran the command in a for loop instead, it might be the *.html that's throwing things:
Code:
for FILE in *.html; do
sed 's/..\/white.jpg/34568\/jpg\/white.jpg/g' $FILE
done
This is a dump of me running the command on a number of test files:
Code:
pwc101@archie:~/tmp/examples> sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
pwc101@archie:~/tmp/examples> ll
total 16K
-rw-r--r-- 1 pwc101 users 101 2008-02-27 21:14 test
-rw-r--r-- 1 pwc101 users 171 2008-02-27 21:14 test2
-rw-r--r-- 1 pwc101 users 182 2008-02-27 21:15 test3
-rw-r--r-- 1 pwc101 users 171 2008-02-27 21:15 test3y
pwc101@archie:~/tmp/examples> for i in test*; do echo $i:; cat $i; echo; done
test:
this is a test file
./some/path/file.jpg
../white.jpg
../white/file.jpg
some more text
/another/path
test2:
this is a test file
./some/path/file.jpg
../white.jpg
../white/file.jpg
some more text
/another/path
adding in
more text
this is a test
\checking@odd-characters%$
test3:
more tests
this is a test file
./some/path/file.jpg
../white.jpg
../white/file.jpg
some more text
/another/path
adding in
more text
this is a test
\checking@odd-characters%$
test3y:
this is a test file
./some/path/file.jpg
../white.jpg
../white/file.jpg
some more text
/another/path
adding in
more text
this is a test
\checking@odd-characters%$
pwc@archibald:~/tmp/examples> sed -i 's/..\/white.jpg/34568\/jpg\/white.jpg/g' test*
pwc101@archie:~/tmp/examples> for i in test*; do echo $i:; cat $i; done
test:
this is a test file
./some/path/file.jpg
34568/jpg/white.jpg
../white/file.jpg
some more text
/another/path
test2:
this is a test file
./some/path/file.jpg
34568/jpg/white.jpg
../white/file.jpg
some more text
/another/path
adding in
more text
this is a test
\checking@odd-characters%$
test3:
more tests
this is a test file
./some/path/file.jpg
34568/jpg/white.jpg
../white/file.jpg
some more text
/another/path
adding in
more text
this is a test
\checking@odd-characters%$
test3y:
this is a test file
./some/path/file.jpg
34568/jpg/white.jpg
../white/file.jpg
some more text
/another/path
adding in
more text
this is a test
\checking@odd-characters%$
pwc101@archie:~/tmp/examples>
I've put the changed parts in bold to show it seems to have worked here.
Also, what sed are you using (phantom_cyph)? The -i flag for in-place editing is a non-standard GNU option, and may not be present on Solaris or BSD sed. In that case, you will need to redirect to a temporary file manually and then replace the original file with the temporary file.
Btw, the in-place flag is cross-platform, standard perl:
Also, what sed are you using (phantom_cyph)? The -i flag for in-place editing is a non-standard GNU option, and may not be present on Solaris or BSD sed. In that case, you will need to redirect to a temporary file manually and then replace the original file with the temporary file.
Btw, the in-place flag is cross-platform, standard perl:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.