<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Changing a filename to a regex pattern in a shell script in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857262#M96608</link>
    <description>&lt;!--!*#--&gt;How about a sed construct in your for loop:&lt;BR /&gt;&lt;BR /&gt;for a in `ls $DIR`&lt;BR /&gt;do&lt;BR /&gt;    echo $a | sed -n '/^img_19981117_001_[0-9]*x[0-9]*\.jpg$/p'&lt;BR /&gt;done</description>
    <pubDate>Tue, 05 Sep 2006 17:15:57 GMT</pubDate>
    <dc:creator>Sandman!</dc:creator>
    <dc:date>2006-09-05T17:15:57Z</dc:date>
    <item>
      <title>Changing a filename to a regex pattern in a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857260#M96606</link>
      <description>I have a image gallery where files are named in the format img_yyyymmdd_nnn_widthxheight.jpg, for example: img_19981117_001_320x240.jpg&lt;BR /&gt;&lt;BR /&gt;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).&lt;BR /&gt;&lt;BR /&gt;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?&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;&lt;BR /&gt;DIR="$1"&lt;BR /&gt;&lt;BR /&gt;if [ "$DIR" = "" ] ; then&lt;BR /&gt;  echo "ERROR: You need to specify dir."&lt;BR /&gt;  exit -1;&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;for a in `ls $DIR`&lt;BR /&gt;do&lt;BR /&gt;  if [ ! -f "${DIR}/${a}/IMAGEDESC" ] ; then&lt;BR /&gt;    echo "Creating IMAGEDESC in ${DIR}/${a}";&lt;BR /&gt;    for b in `ls $DIR/$a/thumb`&lt;BR /&gt;    do&lt;BR /&gt;      echo "/${b}/ No Description" &amp;gt;&amp;gt; "${DIR}/${a}/IMAGEDESC"&lt;BR /&gt;    done;&lt;BR /&gt;  fi&lt;BR /&gt;done;&lt;BR /&gt;&lt;BR /&gt;exit 0;</description>
      <pubDate>Tue, 05 Sep 2006 15:50:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857260#M96606</guid>
      <dc:creator>Jonas Kvinge</dc:creator>
      <dc:date>2006-09-05T15:50:27Z</dc:date>
    </item>
    <item>
      <title>Re: Changing a filename to a regex pattern in a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857261#M96607</link>
      <description>Hi Jonas:&lt;BR /&gt;&lt;BR /&gt;Do you mean that you have a file that looks something like:&lt;BR /&gt;&lt;BR /&gt;img_19981117_001_320x240.jpg&lt;BR /&gt;img_20001117abcd_320x240.jpg&lt;BR /&gt;img_20001117efgh_640x1400.jpg&lt;BR /&gt;&lt;BR /&gt;...and you want:&lt;BR /&gt;&lt;BR /&gt;img_19981117_001_[0-9]*x[0-9]*.jpg&lt;BR /&gt;img_20001117abcd_[0-9]*x[0-9]*.jpg&lt;BR /&gt;img_20001117efgh_[0-9]*x[0-9]*.jpg&lt;BR /&gt;&lt;BR /&gt;...if so:&lt;BR /&gt;&lt;BR /&gt;# perl -ple 's%(.+_)(\d+)x(\d+)\.jpg%$1\[0-9]*x\[0-9]*.jpg%' file&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Tue, 05 Sep 2006 16:23:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857261#M96607</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2006-09-05T16:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: Changing a filename to a regex pattern in a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857262#M96608</link>
      <description>&lt;!--!*#--&gt;How about a sed construct in your for loop:&lt;BR /&gt;&lt;BR /&gt;for a in `ls $DIR`&lt;BR /&gt;do&lt;BR /&gt;    echo $a | sed -n '/^img_19981117_001_[0-9]*x[0-9]*\.jpg$/p'&lt;BR /&gt;done</description>
      <pubDate>Tue, 05 Sep 2006 17:15:57 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857262#M96608</guid>
      <dc:creator>Sandman!</dc:creator>
      <dc:date>2006-09-05T17:15:57Z</dc:date>
    </item>
    <item>
      <title>Re: Changing a filename to a regex pattern in a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857263#M96609</link>
      <description>Jonas,&lt;BR /&gt;&lt;BR /&gt;If I have understood your problem correctly&lt;BR /&gt;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.&lt;BR /&gt;Assuming that the b variable is the filename of the format img_yyyymmdd_nnn_widthxheight.jpg in the listing from the $DIR/$a/thumb&lt;BR /&gt;You can define newwidth and newheight as variable and define a value or hardcode the values.&lt;BR /&gt;&lt;BR /&gt;#!/bin/sh&lt;BR /&gt;&lt;BR /&gt;DIR="$1"&lt;BR /&gt;&lt;BR /&gt;if [ "$DIR" = "" ] ; then&lt;BR /&gt;echo "ERROR: You need to specify dir."&lt;BR /&gt;exit -1;&lt;BR /&gt;fi&lt;BR /&gt;newwidth=640  # define a value for newwidth&lt;BR /&gt;newheight=800 # define a value for newheight&lt;BR /&gt;&lt;BR /&gt;for a in `ls $DIR`&lt;BR /&gt;do&lt;BR /&gt;if [ ! -f "${DIR}/${a}/IMAGEDESC" ] ; then&lt;BR /&gt;echo "Creating IMAGEDESC in ${DIR}/${a}";&lt;BR /&gt;for b in `ls $DIR/$a/thumb`&lt;BR /&gt;do&lt;BR /&gt;echo "${b%_*.jpg}_${newwidth}x${newheight}.jpg"" &amp;gt;&amp;gt; "${DIR}/${a}/IMAGEDESC"  # If you use the values thru newwidth and newheight variables&lt;BR /&gt;# echo "${b%_*.jpg}_640x800.jpg"" &amp;gt;&amp;gt; "${DIR}/${a}/IMAGEDESC" # If you want to directly specify the new resolution. Remove the # at the beginning and hash the above line&lt;BR /&gt;done;&lt;BR /&gt;fi&lt;BR /&gt;done;&lt;BR /&gt;&lt;BR /&gt;exit 0;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Ninad</description>
      <pubDate>Tue, 05 Sep 2006 23:38:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857263#M96609</guid>
      <dc:creator>Ninad_1</dc:creator>
      <dc:date>2006-09-05T23:38:01Z</dc:date>
    </item>
    <item>
      <title>Re: Changing a filename to a regex pattern in a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857264#M96610</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;So the lines should be &lt;BR /&gt;echo "${b%_*.jpg}_${newwidth}x${newheight}.jpg" &amp;gt;&amp;gt; "${DIR}/${a}/IMAGEDESC" # If you use the values thru newwidth and newheight variables&lt;BR /&gt;# echo "${b%_*.jpg}_640x800.jpg" &amp;gt;&amp;gt; "${DIR}/${a}/IMAGEDESC" # If you want to directly specify the new resolution. Remove the # at the beginning and hash the above line&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Ninad</description>
      <pubDate>Wed, 06 Sep 2006 01:34:01 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857264#M96610</guid>
      <dc:creator>Ninad_1</dc:creator>
      <dc:date>2006-09-06T01:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: Changing a filename to a regex pattern in a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857265#M96611</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;I would try to split your filename using shell builtin string operators:&lt;BR /&gt;img=img_19981117_001_320x240.jpg&lt;BR /&gt;img_ident=${img%_*}&lt;BR /&gt;img_res=${img##*_}&lt;BR /&gt;print $img_ident&lt;BR /&gt;img_19981117_001&lt;BR /&gt;print $img_res&lt;BR /&gt;320x240.jpg&lt;BR /&gt;&lt;BR /&gt;Now catenate your new filenames, e.g.:&lt;BR /&gt;&lt;BR /&gt;touch img_19981117_001_320x240.jpg img_19981117_001_640x480.jpg&lt;BR /&gt;ls ${img_ident}_*.jpg&lt;BR /&gt;img_19981117_001_320x240.jpg  img_19981117_001_640x480.jpg&lt;BR /&gt;&lt;BR /&gt;or&lt;BR /&gt;low=320x240&lt;BR /&gt;med=640x480&lt;BR /&gt;hi=1280=960&lt;BR /&gt;img_medium=${img_ident}_$med.jpg&lt;BR /&gt;&lt;BR /&gt;mfG Peter</description>
      <pubDate>Thu, 07 Sep 2006 11:11:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857265#M96611</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-09-07T11:11:03Z</dc:date>
    </item>
    <item>
      <title>Re: Changing a filename to a regex pattern in a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857266#M96612</link>
      <description>James,&lt;BR /&gt;&lt;BR /&gt;Thank you. You were the only one who gave a solution that worked.&lt;BR /&gt;&lt;BR /&gt;Thanks to everyone else who tried to help me out.&lt;BR /&gt;&lt;BR /&gt;My final script (mkgallery.sh) can be found in my photogallery package found on &lt;A href="http://www.night-light.net/photogallery/" target="_blank"&gt;http://www.night-light.net/photogallery/&lt;/A&gt; released under GPL.&lt;BR /&gt;</description>
      <pubDate>Sun, 19 Nov 2006 14:32:32 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857266#M96612</guid>
      <dc:creator>Jonas Kvinge</dc:creator>
      <dc:date>2006-11-19T14:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Changing a filename to a regex pattern in a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857267#M96613</link>
      <description>Oh fine - zero points, how generous!</description>
      <pubDate>Sun, 19 Nov 2006 15:28:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857267#M96613</guid>
      <dc:creator>Peter Nikitka</dc:creator>
      <dc:date>2006-11-19T15:28:38Z</dc:date>
    </item>
    <item>
      <title>Re: Changing a filename to a regex pattern in a shell script</title>
      <link>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857268#M96614</link>
      <description>Peter,&lt;BR /&gt;&lt;BR /&gt;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.</description>
      <pubDate>Sun, 19 Nov 2006 16:23:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/changing-a-filename-to-a-regex-pattern-in-a-shell-script/m-p/3857268#M96614</guid>
      <dc:creator>Jonas Kvinge</dc:creator>
      <dc:date>2006-11-19T16:23:09Z</dc:date>
    </item>
  </channel>
</rss>

