[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:
>> 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





Reply to: