- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Changing a filename to a regex pattern in a shell ...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2006 08:50 AM
тАО09-05-2006 08:50 AM
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;
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2006 09:23 AM
тАО09-05-2006 09:23 AM
SolutionDo 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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2006 10:15 AM
тАО09-05-2006 10:15 AM
Re: Changing a filename to a regex pattern in a shell script
for a in `ls $DIR`
do
echo $a | sed -n '/^img_19981117_001_[0-9]*x[0-9]*\.jpg$/p'
done
- Tags:
- sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2006 04:38 PM
тАО09-05-2006 04:38 PM
Re: Changing a filename to a regex pattern in a shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-05-2006 06:34 PM
тАО09-05-2006 06:34 PM
Re: Changing a filename to a regex pattern in a shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-07-2006 04:11 AM
тАО09-07-2006 04:11 AM
Re: Changing a filename to a regex pattern in a shell script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-19-2006 06:32 AM
тАО11-19-2006 06:32 AM
Re: Changing a filename to a regex pattern in a shell script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-19-2006 07:28 AM
тАО11-19-2006 07:28 AM
Re: Changing a filename to a regex pattern in a shell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-19-2006 08:23 AM
тАО11-19-2006 08:23 AM
Re: Changing a filename to a regex pattern in a shell script
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.