[opensuse] Bash - sed variable help needed
Listmates, I am trying to translate filenames for our digital camera pictures. The problem being that we have 2 identical Kodak digital cameras and they each store pictures in the filename format 100_1234.jpg To avoid the duplicate filename problems that will occur (100_1235 and from the other 100_1235) I want a quick script to translate the filenames on one camera to something other than 100_xxx. I have a small script that can do that, but instead of just a static change to 101_xxx, I would like to change the 100 into something more useful, like a date. The problem is that I cannot get sed to allow substitution of a value contained in the sed expression. So far, I can do: for i in $(ls); do ORIG_FNAME=$i NEW_FNAME=$(echo $i | sed -e 's/^100/101zd/') which is fine. What I want to do is something like: for i in $(ls); do ORIG_FNAME=$i NEW_FNAME=$(echo $i | sed -e 's/^100/`date +%Y%m%d`/') This is where the trouble comes in. I can't see a way to accomplish this. Anybody have any ideas? I guess I could turn the problem around and grab the numerical part on the right side of the _ with sed and then concatenate the date to that. However, if this is a way to work a variable into sed, that would be great. Any ideas? -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sat, 16 Aug, 2008 at 02:14:03 -0500, David C. Rankin wrote:
What I want to do is something like:
for i in $(ls); do
ORIG_FNAME=$i NEW_FNAME=$(echo $i | sed -e 's/^100/`date +%Y%m%d`/')
This is where the trouble comes in. I can't see a way to accomplish this. Anybody have any ideas? I guess I could turn the problem around and grab the numerical part on the right side of the _ with sed and then concatenate the date to that. However, if this is a way to work a variable into sed, that would be great. Any ideas?
Use double quotes instead; ... | sed -e "s/^100/$(date +%Y%m%d)/" hth /jon -- YMMV -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Jon Clausen wrote:
On Sat, 16 Aug, 2008 at 02:14:03 -0500, David C. Rankin wrote:
What I want to do is something like:
for i in $(ls); do
ORIG_FNAME=$i NEW_FNAME=$(echo $i | sed -e 's/^100/`date +%Y%m%d`/')
This is where the trouble comes in. I can't see a way to accomplish this. Anybody have any ideas? I guess I could turn the problem around and grab the numerical part on the right side of the _ with sed and then concatenate the date to that. However, if this is a way to work a variable into sed, that would be great. Any ideas?
Use double quotes instead;
... | sed -e "s/^100/$(date +%Y%m%d)/"
hth /jon
Excellent! I knew it had to the simple. Thanks jon! -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 16 August 2008 00:18, Jon Clausen wrote:
On Sat, 16 Aug, 2008 at 02:14:03 -0500, David C. Rankin wrote:
What I want to do is something like:
for i in $(ls); do
ORIG_FNAME=$i NEW_FNAME=$(echo $i | sed -e 's/^100/`date +%Y%m%d`/')
This is where the trouble comes in. I can't see a way to accomplish this. Anybody have any ideas? I guess I could turn the problem around and grab the numerical part on the right side of the _ with sed and then concatenate the date to that. However, if this is a way to work a variable into sed, that would be great. Any ideas?
Use double quotes instead;
... | sed -e "s/^100/$(date +%Y%m%d)/"
The difference between single and double quotes in shell scripting is a key distinction to be aware of. Additionally, it's probably worthwhile to put the date-stamp string into a variable and use that in the sed invocation in order to avoid repeatedly invoking the "date" command to get the same string (unless the command runs across midnight, of course). % dateStamp="$(date +%Y%m%d)" ... % ... |sed -e "$s/^100/$dateStamp/"
hth /jon -- YMMV
Randall Schulz -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David C. Rankin wrote:
Listmates,
What I want to do is something like:
for i in $(ls); do
ORIG_FNAME=$i NEW_FNAME=$(echo $i | sed -e 's/^100/`date +%Y%m%d`/')
This is where the trouble comes in. I can't see a way to accomplish this. Anybody have any ideas? I guess I could turn the problem around and grab the numerical part on the right side of the _ with sed and then concatenate the date to that. However, if this is a way to work a variable into sed, that would be great. Any ideas?
I took the easy way out... for i in $(ls); do ORIG_FNAME=$i PICNUM=$(echo $i | sed -e 's/^100//') NEW_FNAME="$TODAY$PICNUM" mv $ORIG_FNAME $NEW_FNAME -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Sat, 16 Aug 2008, by drankinatty@suddenlinkmail.com:
David C. Rankin wrote:
Listmates, What I want to do is something like: for i in $(ls); do ORIG_FNAME=$i NEW_FNAME=$(echo $i | sed -e 's/^100/`date +%Y%m%d`/') This is where the trouble comes in. I can't see a way to accomplish this. Anybody have any ideas? I guess I could turn the problem around and grab the numerical part on the right side of the _ with sed and then concatenate the date to that. However, if this is a way to work a variable into sed, that would be great. Any ideas?
I took the easy way out...
for i in $(ls); do
ORIG_FNAME=$i PICNUM=$(echo $i | sed -e 's/^100//')
NEW_FNAME="$TODAY$PICNUM" mv $ORIG_FNAME $NEW_FNAME
There is a tool for this you know (not surprisingly, this is
Linux..), look at exiftool(1)
I think you could use one of these examples from the manpage.
exiftool '-FileName
Theo v. Werkhoven wrote:
There is a tool for this you know (not surprisingly, this is Linux..), look at exiftool(1)
I think you could use one of these examples from the manpage.
exiftool '-FileName
exiftool -r '-FileName
exiftool '-FileName<${CreateDate}_$filenumber.jpg' -d %Y%m%d dir/*.jpg Set the filename of all JPG images in the current directory from the CreateDate and FileNumber tags, in the form "20060507_118-1861.jpg".
Theo
Thank you Theo! I think you just saved me about 1000 hours of frustraton! -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sat, 16 Aug 2008, David C. Rankin wrote:- While you've already had some answers, here's a few other tips.
The problem is that I cannot get sed to allow substitution of a value contained in the sed expression. So far, I can do:
for i in $(ls); do
ORIG_FNAME=$i NEW_FNAME=$(echo $i | sed -e 's/^100/`date +%Y%m%d`/') ^ ^ These aren't the same as using double-quotes. Bash won't do expansion or execution within single quoted strings.
Also, there's no need for the "echo | sed" combination. You could use: DATE=$(date +%Y%m%s) for ORIG_FNAME in * do if [ "${ORIG_FNAME::3}" == "100" ] # check first three characters of $ORIG_FNAME then NEW_FNAME=${DATE}${ORIG_FNAME:3} else continue # skip this file fi # if a file with the new name exists, skip it, else rename # [ ! -e "${NEW_FNAME}" ] && mv "${ORIG_FNAME}" "${NEW_FNAME}" Regards, David Bolt -- Team Acorn: http://www.distributed.net/ OGR-P2 @ ~100Mnodes RC5-72 @ ~15Mkeys SUSE 10.1 32 | | openSUSE 10.3 32b | openSUSE 11.0 32b | openSUSE 10.2 64b | openSUSE 10.3 64b | openSUSE 11.0 64b RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC | RISC OS 3.11 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Saturday 16 August 2008 18:06:40 David Bolt wrote:
On Sat, 16 Aug 2008, David C. Rankin wrote:-
While you've already had some answers, here's a few other tips.
The problem is that I cannot get sed to allow substitution of a value contained in the sed expression. So far, I can do:
for i in $(ls); do
ORIG_FNAME=$i NEW_FNAME=$(echo $i | sed -e 's/^100/`date +%Y%m%d`/')
^ ^ These aren't the same as using double-quotes. Bash won't do expansion or execution within single quoted strings.
Also, there's no need for the "echo | sed" combination. You could use:
DATE=$(date +%Y%m%s) for ORIG_FNAME in * do if [ "${ORIG_FNAME::3}" == "100" ] # check first three characters of $ORIG_FNAME then NEW_FNAME=${DATE}${ORIG_FNAME:3} else continue # skip this file fi
Why not for ORIG_FNAME in 100* do :) Anders -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Sat, 16 Aug 2008, Anders Johansson wrote:- <snip>
Why not
for ORIG_FNAME in 100* do
:)
Didn't think about that :/ Regards, David Bolt -- Team Acorn: http://www.distributed.net/ OGR-P2 @ ~100Mnodes RC5-72 @ ~15Mkeys SUSE 10.1 32 | | openSUSE 10.3 32b | openSUSE 11.0 32b | openSUSE 10.2 64b | openSUSE 10.3 64b | openSUSE 11.0 64b RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC | RISC OS 3.11 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (6)
-
Anders Johansson
-
David Bolt
-
David C. Rankin
-
Jon Clausen
-
Randall R Schulz
-
Theo v. Werkhoven