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

Re: how to match parenthesis in sed



H.S. wrote:
> H.S. wrote:
>> H.S. wrote:
>>> H.S. wrote:
>>>> Hello,
>>>>
>>>> I am renaming files by removing parentheses from them. These are image
>>>> files which were sorted in Windows (by placing them in the desired order
>>>> and then by renaming them in batch mode in Windows explorer -- couldn't
>>>> do this in Linux).
>>>>
>>>> Here is an example:
>>>> 20030113_Lohri_ (107).jpg   -> is to be named as:
>>>> 20030113_Lohri_ 107.jpg
>>>>
>>>> This I can accomplish by:
>>>> $> nn=`echo "20030113_Lohri_ (107).jpg" | tr -d "()"`; echo $nn
>>>>
>>>> which spits out the modified name (in variable nn) and the output is:
>>>> 20030113_Lohri_ 107.jpg
>>>>
>>>> However, I also want to remove that space preceding the opening
>>>> parenthesis in the input filename. I am trying to do this via sed,
>>>> something like (the following is one long line):
>>>> $> nn=`echo "20030113_Lohri_ (107).jpg" | sed -e
>>>> s/\(.*\)\.[Jj][Pp][Gg]/\1.jpg/`;
>>>>
>>>> However, this is not working. The above should give just the basename
>>>> and then I add an extension of ".jpg" to it. I am guessing the
>>>> parentheses in the input filename is causing the problem. How do I match
>>>>  parenthesis in sed then?
>>>>
>>>> thanks,
>>>> ->HS
>>>>
>>>>
>>> There was a mistake in my earlier sed command. The following seems to work:
>>> nn2=`echo "$fn" | sed -e 's%\(.*\)\
>>> (\([0-9][0-9][0-9]\))\.[Jj][Pp][Gg]%\\1\2.jpg%'`;
>>>
>>> where $fn is the input filename and $nn2 will be the new filename.
>>>
>>> ->HS
>>>
>>>
>>>
>> Furthermore, I just notice, there may or may not be a whitespace just
>> before the first opening parenthesis in the input filename. To remove
>> that if it exists and to leave it alone if it doesn't, the following
>> seems to work:
>> $> nn2=`echo "$fn" | sed -e
>> 's%\(.*\)\s*(\([0-9]\{3,3\}\))\.[Jj][Pp][Gg]%\\1\2.jpg%'`;
>>
>> where \s* matches zero or more whitespace.
>>
>> Suggestions for improvement?
>>
>> ->HS
>>
>>
>>
>>
> 
> Ah well, I might give the actual full script I am going to use:
> 
> Here is the script that changes the filename:
> ----------------- delparen.sh --------------
> #!/bin/bash
> 
> if [ $# -lt 1 ]; then
> echo "Missing filename. Usage is:";
> echo "$0 <filename>";
> exit -1;
> fi
> 
> #get the filename
> fn=$1;
> #get new filename (following is one long line)
> nn2=`echo "$fn" | sed -e
> 's%\(.*\)\s*(\([0-9]\{3,3\}\))\.[Jj][Pp][Gg]%\\1\2.jpg%'`;
> #tell what is being done
> echo "$fn --to--> $nn2";
> #do the change
> mv "$fn" "$nn2";
> 
> 
> exit 0
> ----------------------------------------------
> 
> And here is the command that finds all my image files in directory E and
> renames them using the above script:
> $> find E/ -name "*.[Jj][Pp][Gg]" -exec ./delparen.sh "{}" \;

Sorry for all this bunch of emails. That whitespace is not being dealt
with properly here. I guess I just being impatient. :)

Suggestion on how to do that?

->HS



Reply to: