Operating System - Linux
1753886 Members
7623 Online
108809 Solutions
New Discussion юеВ

Changing a filename to a regex pattern in a shell script

 
SOLVED
Go to solution
Jonas Kvinge
Occasional Advisor

Changing a filename to a regex pattern in a shell script

I have a image gallery where files are named in the format img_yyyymmdd_nnn_widthxheight.jpg, for example: img_19981117_001_320x240.jpg

I've written a script that creates a list of all the images in a file. But I need to replace the filename of the image with a regex pattern so it applies to same image with a diffrent size. (Since different sizes of the image have different filename).

How can I replace the image filename (img_19981117_001_320x240.jpg) with a regex pattern of the filename like: /^img_19981117_001_[0-9]*x[0-9]*\.jpg$/ using sed or awk?

#!/bin/sh

DIR="$1"

if [ "$DIR" = "" ] ; then
echo "ERROR: You need to specify dir."
exit -1;
fi

for a in `ls $DIR`
do
if [ ! -f "${DIR}/${a}/IMAGEDESC" ] ; then
echo "Creating IMAGEDESC in ${DIR}/${a}";
for b in `ls $DIR/$a/thumb`
do
echo "/${b}/ No Description" >> "${DIR}/${a}/IMAGEDESC"
done;
fi
done;

exit 0;
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: Changing a filename to a regex pattern in a shell script

Hi Jonas:

Do you mean that you have a file that looks something like:

img_19981117_001_320x240.jpg
img_20001117abcd_320x240.jpg
img_20001117efgh_640x1400.jpg

...and you want:

img_19981117_001_[0-9]*x[0-9]*.jpg
img_20001117abcd_[0-9]*x[0-9]*.jpg
img_20001117efgh_[0-9]*x[0-9]*.jpg

...if so:

# perl -ple 's%(.+_)(\d+)x(\d+)\.jpg%$1\[0-9]*x\[0-9]*.jpg%' file

Regards!

...JRF...
Sandman!
Honored Contributor

Re: Changing a filename to a regex pattern in a shell script

How about a sed construct in your for loop:

for a in `ls $DIR`
do
echo $a | sed -n '/^img_19981117_001_[0-9]*x[0-9]*\.jpg$/p'
done
Ninad_1
Honored Contributor

Re: Changing a filename to a regex pattern in a shell script

Jonas,

If I have understood your problem correctly
i.e. you have filename with a particular image resolution and now you want to create a list of files by listing the existing files and renaming the img_yyyymmdd_nnn_widthxheight.jpg file with img_yyyymmdd_nnn_newwidthxnewheight.jpg then here is the solution.
Assuming that the b variable is the filename of the format img_yyyymmdd_nnn_widthxheight.jpg in the listing from the $DIR/$a/thumb
You can define newwidth and newheight as variable and define a value or hardcode the values.

#!/bin/sh

DIR="$1"

if [ "$DIR" = "" ] ; then
echo "ERROR: You need to specify dir."
exit -1;
fi
newwidth=640 # define a value for newwidth
newheight=800 # define a value for newheight

for a in `ls $DIR`
do
if [ ! -f "${DIR}/${a}/IMAGEDESC" ] ; then
echo "Creating IMAGEDESC in ${DIR}/${a}";
for b in `ls $DIR/$a/thumb`
do
echo "${b%_*.jpg}_${newwidth}x${newheight}.jpg"" >> "${DIR}/${a}/IMAGEDESC" # If you use the values thru newwidth and newheight variables
# echo "${b%_*.jpg}_640x800.jpg"" >> "${DIR}/${a}/IMAGEDESC" # If you want to directly specify the new resolution. Remove the # at the beginning and hash the above line
done;
fi
done;

exit 0;


Regards,
Ninad
Ninad_1
Honored Contributor

Re: Changing a filename to a regex pattern in a shell script

Hi,

On observing my above post carefully - I have put an extra " for the echo "${b%_ .... statement after .jpg (.jpg"" should be corrected to .jpg" ). Please remove the same before execution.

So the lines should be
echo "${b%_*.jpg}_${newwidth}x${newheight}.jpg" >> "${DIR}/${a}/IMAGEDESC" # If you use the values thru newwidth and newheight variables
# echo "${b%_*.jpg}_640x800.jpg" >> "${DIR}/${a}/IMAGEDESC" # If you want to directly specify the new resolution. Remove the # at the beginning and hash the above line


Regards,
Ninad
Peter Nikitka
Honored Contributor

Re: Changing a filename to a regex pattern in a shell script

Hi,

I would try to split your filename using shell builtin string operators:
img=img_19981117_001_320x240.jpg
img_ident=${img%_*}
img_res=${img##*_}
print $img_ident
img_19981117_001
print $img_res
320x240.jpg

Now catenate your new filenames, e.g.:

touch img_19981117_001_320x240.jpg img_19981117_001_640x480.jpg
ls ${img_ident}_*.jpg
img_19981117_001_320x240.jpg img_19981117_001_640x480.jpg

or
low=320x240
med=640x480
hi=1280=960
img_medium=${img_ident}_$med.jpg

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Jonas Kvinge
Occasional Advisor

Re: Changing a filename to a regex pattern in a shell script

James,

Thank you. You were the only one who gave a solution that worked.

Thanks to everyone else who tried to help me out.

My final script (mkgallery.sh) can be found in my photogallery package found on http://www.night-light.net/photogallery/ released under GPL.
Peter Nikitka
Honored Contributor

Re: Changing a filename to a regex pattern in a shell script

Oh fine - zero points, how generous!
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Jonas Kvinge
Occasional Advisor

Re: Changing a filename to a regex pattern in a shell script

Peter,

I really appreciate your time involved trying to help me out. You misunderstood my question, perhaps I explained it bad but you solution was no use to me, perhaps I should still give you a few points since because of your attempt, I don't know how the points thing work since I'm new to the forum. Sorry for any inconvenience caused. I will give you 2 points on your last reply. I hope this doesn't stop you from answering any further questions I might have.