bash handles bytes instead of chars

2008-09-21 Thread ®om

Configuration Information [Automatically generated, do not change]:
Machine: i486
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i486' 
-DCONF_OSTYPE='lin$
uname output: Linux rom-laptop 2.6.24-21-generic #1 SMP Mon Aug 25 
17:32:09 UTC$

Machine Type: i486-pc-linux-gnu

Bash Version: 3.2
Patch Level: 39
Release Status: release

Description:
First of all, the result of locale :
$ locale
LANG=fr_FR.UTF-8
LC_CTYPE="fr_FR.UTF-8"
LC_NUMERIC="fr_FR.UTF-8"
LC_TIME="fr_FR.UTF-8"
LC_COLLATE="fr_FR.UTF-8"
LC_MONETARY="fr_FR.UTF-8"
LC_MESSAGES="fr_FR.UTF-8"
LC_PAPER="fr_FR.UTF-8"
LC_NAME="fr_FR.UTF-8"
LC_ADDRESS="fr_FR.UTF-8"
LC_TELEPHONE="fr_FR.UTF-8"
LC_MEASUREMENT="fr_FR.UTF-8"
LC_IDENTIFICATION="fr_FR.UTF-8"
LC_ALL=

The char € utf8-encoded in hexa :
$ printf € | xxd -p
e282ac

//byte count
$ printf € | wc -c
3

//char count
$ printf € | wc -m
1

//prints the first char
$ printf € | cut -c1 | xxd -p | sed s/0a$//
e2
//should be e282ac

//prints each char
$ printf € | while read -n 1 v; do printf $v | xxd -p; done
e2
82
ac
//should be e282ac






File renaming using SED in BASH

2008-09-21 Thread MisterMuv

Hi,

I am having a problem renaming a files using SED.

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

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

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

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

I get:
bash: 001: command not found
mv: target `(www.somewhere.net).jpg' is not a directory
bash: 002: command not found
mv: target `(www.somewhere.net).jpg' is not a directory
bash: 003: command not found
mv: target `(www.somewhere.net).jpg' is not a directory


What am I doing wrong?

Thankyou all in advance.
-- 
View this message in context: 
http://www.nabble.com/File-renaming-using-SED-in-BASH-tp19598886p19598886.html
Sent from the Gnu - Bash mailing list archive at Nabble.com.





Re: File renaming using SED in BASH

2008-09-21 Thread Bob Proulx
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




Re: File renaming using SED in BASH

2008-09-21 Thread Chris F.A. Johnson
On 2008-09-21, MisterMuv wrote:
>
> Hi,
>
> I am having a problem renaming a files using SED.
>
> 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
>
> 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
>
> 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
>
> However when I incorporate the mv command all isn't well.
>
> for f in *
> do
> chg=echo $f |sed 's:(www.somewhere.net)::'

   That assigns echo to $chg and tries to execute $f. Use command
   substitution.

> mv $f $chg
> done

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


-- 
   Chris F.A. Johnson, webmaster 
   ===
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)