bug-bash
[Top][All Lists]
Advanced

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

Re: File renaming using SED in BASH


From: Bob Proulx
Subject: Re: File renaming using SED in BASH
Date: Sun, 21 Sep 2008 18:25:01 -0600
User-agent: Mutt/1.5.13 (2006-08-11)

MisterMuv wrote:
> I am having a problem renaming a files using SED.

There is 'sed' and there is 'bash' and there are combinations of the
two.  If you were really asking about sed then help-gnu-utils would be
the better place.  But in this case your use of 'sed' was okay but
it really was your use of 'bash' that was the problem. :-}

> The filenames are in the following format: eg
> 
> 001 - GreatPics1 (The Opening) (www.somewhere.net).jpg
> 002 - GreatPics2 (The Closing) (www.somewhere.net).jpg
> 003 - GreatPics3 (The Ending) (www.somewhere.net).jpg

Okay.

> I am wanting to remove contents of the second parenthesis, i.e.
> "(www.somewhere.net)".
>
> So the files would end up like:
>
> 001 - GreatPics1 (The Opening) .jpg
> 002 - GreatPics2 (The Closing) .jpg
> 003 - GreatPics3 (The Ending) .jpg

Sounds good.

> This is what I have so far.
> 
> If I use this to test all is well
> 
> for f in *
> do
> echo $f |sed 's:(www.somewhere.net)::'
> done

You should quote your arguments so that if they contained wildcards
that those wouldn't be expanded to match filenames by mistake.

  $ f="a*"
  $ touch abc
  $ echo $f
  abc
  $ echo "$f"
  a*

> However when I incorporate the mv command all isn't well.
> 
> for f in *
> do
> chg=echo $f |sed 's:(www.somewhere.net)::'
> mv $f $chg
> done

The chg= line is incorrect syntax.  See the bash manual section on
command substitution.

> I get:
> bash: 001: command not found

Because in your version the variable chg is set to "echo" as an
environment variable for the $f command and $f is 001, 002, etc.

Try this:

  for f in *; do
    chg=$(echo "$f" |sed 's: *(www.somewhere.net)::')
    mv "$f" "$chg"
  done

Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]