Script to search and replace in text file - kinda...
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 hope you don't mind, I modified it a little so it would be more generic.
# I have it setup to use row of 4 pics, either
# jpg or gif, so make sure to have a number of
# pics that is divisible by 4.
If there aren't any pics of one type or the other, you will see an error message to the effect there are no files of that type found. But it still proceeds.
# I redirect the output to index.html
# perl images.pl > index.html
Code:
#!/usr/bin/perl
# not a perl-expert - I'm sure there's more elegant solutions :)
use strict; # enforce variable declarations and quoting
my $i;
my $j;
my $new_row = 0;
my @name=sort(`ls *.jpg *.gif`) ;
my $name1;
my $name2;
if ( (scalar @name)%4 !=0) {
print "Wrong number of pics in directory, quitting!\n";
exit;
}
# kudos to David_ross for suggesting the use of here-documents
print <<HEADER_EOF;
<!-- \/ starthtml -->
<html>
<head>
<style TYPE="text/css">
<!--
a:link {color:#993366;}
a:visited {color:#666666;}
a:hover {color:#FF99CC;}
--> </STYLE>
<title></title>
</head>
<body bgcolor="#1B98C2" text="#000000" link="#0000EE" vlink="#551A8B">
<table width="900" border="4" cellpadding="0" cellspacing="0" bordercolorlight="#FF99CC" bordercolordark="#993366" background="#1B98C2" bgproperties="center">
<table width="900" border="0" cellpadding="0" cellspacing="0">
HEADER_EOF
for( $i=0; $i < (scalar @name) ; $i=$i+4){
print "<tr height=\"270\">\n";
for( $j=0; $j<4 ; $j++){
chomp( $name[$i+$j] );
$name1 = $name[$i+$j];
$name2 = $name[$i+$j];
$name2 =~ s/\./\.THUMB\./;
system(`convert -size 20% $name1 -resize 20% $name2`);
print "<td width=\"240\"><center><a href=\"$name1\"><img src=\"$name2\"></a></td>\n";
}
print "</tr>\n";
}
print <<FOOTER_EOF;
</table>
</td></tr></table>
</body>
</html>
FOOTER_EOF
I hope you don't mind, I modified it a little so it would be more generic.
Heh,
thanks for the feedback, but it's actually totally trivial :)
And I just popped in and did this for Jeff to
force myself and use more perl ;)
As for the modifications: I don't think that this
script is particularly original and don't have any
claims towards the source, consider it GNU General
public licensed :D
I've been putzing with this program and I'm not sure how to proceed with this in perl.
When I make an image gallery in konqueror, it makes a thumbs directory, converts the thumbs to png which looks like mypic.png , then it dumps them into the thumbs directory. I can get the thumbs over there ok but it ends up being something like mypic_THUMB.pnggif or mypic_THUMB.pngjpg . Having the thumbs as png is kinda handy because you don't have gif images bouncing all over the html page.
Any ideas?
Code:
#!/usr/bin/perl
# use the command: perl images.pl > index.html
# Set up in rows of 4 pics so you need an amount
# of pics that is evenly divided by 4
use strict; # enforce variable declarations and quoting
mkdir "thumbs", 0777 unless -d "thumbs";
my $i;
my $j;
my $new_row = 0;
my @name=sort(`ls *.jpg *.gif *.png`) ;
my $name1;
my $name2;
if ( (scalar @name)%4 !=0) {
print "Wrong number of pics in directory, quitting!\n";
exit;
}
print <<HEADER_EOF;
<!-- \/ starthtml -->
<html>
<head>
<style TYPE="text/css">
<!--
a:link {color:#FFFFFF;}
a:visited {color:#666666;}
a:hover {color:#1B98C2;}
--> </STYLE>
<title></title>
</head>
<body bgcolor="#FFFFFF" text="#000000" link="#FFFFFF" vlink="#FFFFFF">
<table align=\"center\" border="1" cellpadding="5" cellspacing="2" bordercolorlight="#FF0000" bordercolordark="#993366" background="#1B98C2" bgproperties="center">
<table align=\"center\" width=\"80%\" border="1" cellpadding="5" cellspacing="2">
HEADER_EOF
for($i=0; $i < (scalar @name) ; $i=$i+4){
print "<tr height=\"150\">\n";
for( $j=0; $j<4 ; $j++){
chomp( $name[$i+$j] );
$name1 = $name[$i+$j];
$name2 = $name[$i+$j];
$name2 =~ s/\./_THUMB\./;
system(`convert -geometry x70 $name1 $name2`);
system(`mv $name2 ./thumbs/`);
print "<td width=\"120\"><center><a href=\"$name1\"><img src=\"./thumbs/$name2\"></a><div>$name1</div></td>\n";
}
print "</tr>\n";
}
print <<FOOTER_EOF;
</table>
</td></tr></table>
</body>
</html>
FOOTER_EOF
That was originally $name2 =~ s/\./_THUMB\./;
this works fine with the name of mypic_THUMB.jpg or mypic_THUMB.gif
but I was kinda hoping to get the files converted to png like konqueror does.
The problem comes from my trying to have mixed files like jpg and gif in the same directory.
$name2 =~ s/\.jpg/SMALL\.png/;
and
$name2 =~ s/\.jpg/_THUMB\.png/;
both work if you only want to do jpg files. When I use the Image gallery feature of konqueror, it converts both gif and jpg files to png and puts them in the thumbs directory.
That's why I had it like this...
$name2 =~ s/\./SMALL\.png/;
or
$name2 =~ s/\./_THUMB\.png/;
That converted the jpg files ok but it really got hung up on the gif file. The gif didn't go into the thumbs directory and it did put a bunch of stuff in the images directory.
I put a smaller amount of files in the images directory for testing purposes.
Here's what the images directory gets to look like.
bday3.gif
billbig.gif
bombsquad.jpg
bus.gif
bus_THUMB.png.0
bus_THUMB.png.1
bus_THUMB.png.2
bus_THUMB.png.3
bus_THUMB.png.4
bus_THUMB.png.5
bus_THUMB.png.6
bus_THUMB.png.7
cat1.jpg
cat6.jpg
images.pl
index.html
thumbs
But I need to think about these things, too, I'm a
sysadmin, not a graphics or web-designer ... try
to use "convert -combine" (yes, I had to read the
man-page and experiment for ~ 20 minutes for
this piece of info) ... depending on the type of
animated gif the result may be unsightly (I've only
downloaded and converted ONE).
As for the Konqueror wondering:
a) read the source
or
b) ask on their developer list
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.