Monday, January 19, 2015

Re: [LINUX_Newbies] Help with script and regex.

 

On 19Jan2015 00:17, 'highskywhy@yahoo.de' highskywhy@yahoo.de [LINUX_Newbies] <LINUX_Newbies@yahoogroups.com> wrote:
> I need (want) a script.
> I screwed up renaming the sub-folders in my Pictures folder.
> I want each folder to show the full path up to the Pictures folder
>and to have brackets around each name.
> I recursively named each one based upon it's parent, but it started
>at the top folder and worked down vice the other way around (or
>something weird like that).
> I have a bunch of folders that look like the following:
>
>12«{04}«{04«{2009}}«{04«{2009}«{2009«{PHOTOS}}}«{04«{2009}«{2009«{PHOTOS}}«{2009«{PHOTOS}«{PHOTOS«{Pictures}}}}
>
> What I need to do is:
>
> 1. Check the name to see if it needs to be fixed by seeing if it
>ends with '}}}}'.
> 2. Find the first '}'; find the « before the first '}}'; erase
>everything between the two.
> 3. Repeat using '}}' and '}}}'.
> 4. Repeat using '}}}' and '}}}}'.
> 5. Remove the last three '}}}' from the remaining '}}}}'.
> 6. Add brackets around the first part.
> a. Place '{' at the beginning.
> b. Place '}' before first '«'.
>
> Thus ending up with:
>
>{12}«{04}«{2009}«{PHOTOS}«{Pictures}
>
> Also, if you could explain it to me so I can try to do things myself.

I was going to suggest you use a sed script to generate the new names, then
when convinced it was working, pipe it into a small shell script:

ls \
| sed -f the-sed-script \
| while read -r oldname newname
do if [ -e "$newname" ]
then echo "warning: already exists: $newname" >&2
else echo mv -i -- "$oldname" "$newname" || break
fi
done

The above generates the list of names needing changing with the "ls" command;
it would need to stand in the necessary directory.

The sed script reads the names and emits both the original name and the new
name.

The while loop reads both and issues a "mv -i" to rename the file (the "echo"
is for testing; remove it when you are sure things are good). It breaks out of
the loop if a "mv" fails, allowing you to stop there and see what went wrong.

So to the filename mangling.

Let me say first you that I suspect you're trying to undo too much.

How much do you know about the filenames. Are they _all_ "PHOTOS" and
"Pictures", and you merely need to get the date parts? Or are they more
compilcated and you really do need to undo your string carefully, reversing the
process used to build it?

If you only need to get the dates and know the "PHOTOS" and "Pictures" strings,
you could just grab the year/month/day out of the string and assemble the new
name directly with less mucking about.

My only other remark is that if I were doing what you are doing (naming folders
"date-blah-blah-blah", I would _not_ use the American date order of MM-DD-YYYY,
but the ISO8601 order of YYYY-MM-DD, which sorts much more nicely and does not
have horrible "is-it-a-day/is-it-a-month?" ambiguity. So I'd be aiming for a
name like:

{2009}«{12}«{04}«{PHOTOS}«{Pictures}

I also suspect that when you say "Find the first '}'" you mean the first '}'
which is not part of a longer '}}...' sequence.

Is that the case?

This is going to be fiddly regardless, but I suspect it will be easier in
something like python rather than sed.

If you can clarify all the above questions we can proceed.

Cheers,
Cameron Simpson <cs@zip.com.au>

Be bold. Leave something behind and see if you miss it.
NUX_Newbies- Jeff Lowe, 1996

__._,_.___

Posted by: Cameron Simpson <cs@zip.com.au>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (4)
To unsubscribe from this list, please email LINUX_Newbies-unsubscribe@yahoogroups.com & you will be removed.

.

__,_._,___

No comments:

Post a Comment