Michael Lazin on 16 Nov 2018 07:02:21 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] scripting help, variables in sed


Thank you, I am very familiar with find and exec, but this doesn't accomplish my goal.  Maybe I wasn't clear.  I want a number to be added in front of each file name and that number to be incremented by one so each file ends up with a unique name so they can be put together in a single folder.  I was under the impression that sed -i would accomplish this, but maybe I'm doing this entirely wrong, I want to edit the filename and not the content.  I am sorry, I am comfortable with bash but I am at best  a noob programmer.  Thanks.   

On Fri, Nov 16, 2018 at 9:56 AM brent timothy saner <brent.saner@gmail.com> wrote:
On 11/16/18 9:45 AM, Michael Lazin wrote:
> My girlfriend has a large collection of scanned family photos many of
> which are different photos with the same name.  We eliminated all
> duplicates with fdupes, but she would like to put them all in one folder
> and give it to her sisters on a cd.
>
> I had a bit of free time this morning and tried to write a for loop to
> accomplish this but I'm getting syntax errors and I'm kinda stuck.  Any
> help would be appreciated.  Here is my code so far:
>
>   #!/bin/bash
> var=1
> for i in *.jpg; do sed -i -e "s/"($i)"/"($var++)""($i)/" ; done

so first off, this isn't moving the files anywhere. it's.. not really
even doing anything, really, because you're telling sed to edit the
*contents* of a file (without even specifying a file, at that).

to answer your question indirectly you can use variables in a sed
command thusly:

export somevar="BAR"
sed -i -e "s/FOO/${somevar}/g" somefile

all instances of "FOO" in the file named "somefile" will hitherto be
changed to "BAR".

but you don't want that, because you don't want to be running sed on
JPEG files.

instead, you likely want to do (something like) this:


mkdir -p new/directory
find ./ -type f -name "*.jpg" -exec mv '{}' new/directory/. \;


no need to do sed or ...variable increments, for whatever reason, or
what have you.

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug


--
Michael Lazin

to gar auto estin noein te kai ennai
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug