LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 02-27-2008, 08:49 AM   #1
phantom_cyph
Senior Member
 
Registered: Feb 2007
Location: West Africa
Distribution: Slackware/Debian (when necessary)
Posts: 2,443
Blog Entries: 1

Rep: Reputation: 105Reputation: 105
editing multiple html files at once


I know there is a way to do it, but I can't remember how.

I need to be able to change this line:
Quote:
../white.jpg
to this line:
Quote:
35468/jpg/white.jpg
I have to do it with around 20 files and don't want to do it one by one. How do I do it?
 
Old 02-27-2008, 09:26 AM   #2
bitpicker
Member
 
Registered: Jul 2003
Location: Germany
Distribution: Xubuntu, Ubuntu
Posts: 416
Blog Entries: 14

Rep: Reputation: 35
If you're using Quanta Plus with the KFileReplace plugin you can do that for all files in a given directory for example.

Robin
 
Old 02-27-2008, 10:22 AM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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);
}
Use at your own risk.

HTH

Forrest

Last edited by forrestt; 02-27-2008 at 10:30 AM.
 
Old 02-27-2008, 10:31 AM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Sounds like a job for sed, Batman!
Code:
sed -i 's/..\/white.jpg/34568\/jpg\/white.jpg/g' *.html
Recursively, I suppose this could be done in conjunction with find:
Code:
find ./ -iname "*.html" -exec sed -i 's/..\/white.jpg/34568\/jpg\/white.jpg/g' {} \;
Unless I've misinterpreted the question?...
 
Old 02-27-2008, 10:31 AM   #5
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Oh, you may need to escape the "/" and "." characters in the $find and $replace. I'm not sure, so test it.

Forrest
 
Old 02-27-2008, 10:34 AM   #6
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
Sure pwc101, that would work, but it is WAY to easy a solution

Forrest
 
Old 02-27-2008, 10:42 AM   #7
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by forrestt View Post
Sure pwc101, that would work, but it is WAY to easy a solution

Forrest
Where there's a will, there's a perl-way: http://www.linuxquestions.org/questi...78#post2385378
 
Old 02-27-2008, 04:02 PM   #8
phantom_cyph
Senior Member
 
Registered: Feb 2007
Location: West Africa
Distribution: Slackware/Debian (when necessary)
Posts: 2,443

Original Poster
Blog Entries: 1

Rep: Reputation: 105Reputation: 105
Quote:
Originally Posted by pwc101 View Post
Sounds like a job for sed, Batman!
Code:
sed -i 's/..\/white.jpg/34568\/jpg\/white.jpg/g' *.html
This would be ideal, but it doesn't seem to work. Essentially the base of this command is:
Code:
sed -i 's/foo/foo_bar/g' somefile.module
But, both of these don't actually change the files. I have tried the following:

Code:
sed -i 's/../white.jpg/34568/jpg/white.jpg/g' *.html
Code:
sed -i 's/..\/white.jpg/34568\/jpg\/white.jpg/g' *.html
Any ideas why it might not work? Is it because of the "/"s in the pathnames?
 
Old 02-27-2008, 04:24 PM   #9
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by phantom_cyph View Post
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.
 
Old 02-27-2008, 09:11 PM   #10
phantom_cyph
Senior Member
 
Registered: Feb 2007
Location: West Africa
Distribution: Slackware/Debian (when necessary)
Posts: 2,443

Original Poster
Blog Entries: 1

Rep: Reputation: 105Reputation: 105
I'm sorry, but I'm not really following. How would I set it up do it, lets say, one file at a time, just changing the filename?

For example: enter the command to change ../white.jpg to 34568/jpg/white.jpg in the file "security.html".

Then, I simply hit the up key, and change the filename to "contact.html".

Sorry for my ignorance, I just can't seem to get it.
 
Old 02-27-2008, 10:31 PM   #11
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 77
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:
Code:
perl -pi -e 's|../white.jpg|34568/jpg/white.jpg|g' foo.html
 
Old 02-28-2008, 12:08 AM   #12
phantom_cyph
Senior Member
 
Registered: Feb 2007
Location: West Africa
Distribution: Slackware/Debian (when necessary)
Posts: 2,443

Original Poster
Blog Entries: 1

Rep: Reputation: 105Reputation: 105
Quote:
Originally Posted by osor View Post
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:
Code:
perl -pi -e 's|../white.jpg|34568/jpg/white.jpg|g' foo.html
Thank God! It worked! I just put an * in front of the .html, and it changed everything in the folder.

Thank-you!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Merge Of Html Files Into A Single Html (or Pdf) fiomba Linux - Software 10 05-11-2018 12:28 PM
xemacs for editing HTML ciden Linux - Newbie 3 08-24-2007 02:46 PM
is there an html editor with remote files editing capabilities? odysseus.lost Linux - Software 2 06-16-2005 08:23 AM
editing multiple files in linux? vinoj Programming 4 11-02-2004 03:31 AM
how to print multiple html files at once? coontie Linux - Software 0 07-01-2003 11:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:14 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration