PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > rm files not matching (tricky) !!
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

rm files not matching (tricky) !!

Réponse
 
LinkBack Outils de la discussion
Vieux 09/09/2007, 04h24   #1
onkar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut rm files not matching (tricky) !!

I have large number of files in a directory with filenames
containing

(1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
might be 1232344saasderv while others might have name awer345345ds
and sdfs12312 and 1a4d56a345d3 .. any combination ) ,

and

(2) some files containing only digits [0-9] .


I want to write a script removing only those files that contain both
alphabets and digits. One trick might me to use only property (2) to
remove files with property (1). (this is like grep -v grep )

Please me /

  Réponse avec citation
Vieux 09/09/2007, 05h03   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rm files not matching (tricky) !!

On 2007-09-09, onkar wrote:
> I have large number of files in a directory with filenames
> containing
>
> (1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
> might be 1232344saasderv while others might have name awer345345ds
> and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
>
> and
>
> (2) some files containing only digits [0-9] .
>
>
> I want to write a script removing only those files that contain both
> alphabets and digits.


rm -f *[a-z]*[0-9]* *[0-9]*[a-z]*

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 09/09/2007, 05h26   #3
onkar
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rm files not matching (tricky) !!

On Sep 9, 9:03 am, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
> On 2007-09-09, onkar wrote:
> > I have large number of files in a directory with filenames
> > containing

>
> > (1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
> > might be 1232344saasderv while others might have name awer345345ds
> > and sdfs12312 and 1a4d56a345d3 .. any combination ) ,

>
> > and

>
> > (2) some files containing only digits [0-9] .

>
> > I want to write a script removing only those files that contain both
> > alphabets and digits.

>
> rm -f *[a-z]*[0-9]* *[0-9]*[a-z]*
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence


ok !! thats correct but I was interested in knowing how to invert. I
mean how to use property (2) that all-digit filename can be used to
remove the files whose filenames contain both digits and alphabets. I
woiuld be greatful if you can me with that ..


  Réponse avec citation
Vieux 09/09/2007, 11h04   #4
Michal Nazarewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rm files not matching (tricky) !!


>> On 2007-09-09, onkar wrote:
>>> I have large number of files in a directory with filenames
>>> containing (1) numbers [0-9] and alphabets [a-z] (for e.g., one file
>>> name might be 1232344saasderv while others might have name
>>> awer345345ds and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
>>> and (2) some files containing only digits [0-9] .
>>>
>>> I want to write a script removing only those files that contain both
>>> alphabets and digits.


> On Sep 9, 9:03 am, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
>> rm -f *[a-z]*[0-9]* *[0-9]*[a-z]*


rm -f -- *[a-z]*[0-9]* *[0-9]*[a-z]*

onkar <onkar.n.m@gmail.com> writes:
> ok !! thats correct but I was interested in knowing how to invert. I
> mean how to use property (2) that all-digit filename can be used to
> remove the files whose filenames contain both digits and alphabets. I
> woiuld be greatful if you can me with that ..


As far as I understand your question you cannot do such a thing. As far
as I know you cannot test if file name contains only digits -- you can
only test if file does not contain any other character, ie. you can
create set of files which do not have property (2) but to create set of
files which have property (2) you'd need to iterate through all files
and exclude those which do not have property (2).

To remove all files without property 2:

rm -f -- *[!0-9]*

But to remove all files with property 2:

for file in *; do
case $file in (*[!0-9]*) continue ; esac
rm -f -- "$file"
done

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
  Réponse avec citation
Vieux 09/09/2007, 11h31   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rm files not matching (tricky) !!

2007-09-09, 12:04(+02), Michal Nazarewicz:
[...]
> As far as I understand your question you cannot do such a thing. As far
> as I know you cannot test if file name contains only digits

[...]

Not with standard globbing, but with ksh globbing (enabled in
bash with shopt -s extglob and in zsh with setopt kshglob) or
with zsh globbing (with extendedglob), you can.

ksh: *([0-9])
zsh: [0-9]# or <-> (<x-y> being positive decimal integers
ranging from x to y).

--
Stéphane
  Réponse avec citation
Vieux 09/09/2007, 13h49   #6
Axel Schlicht
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rm files not matching (tricky) !!

On Sat, 08 Sep 2007 20:24:38 -0700, onkar wrote:

> I have large number of files in a directory with filenames
> containing
>
> (1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
> might be 1232344saasderv while others might have name awer345345ds
> and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
>
> and
>
> (2) some files containing only digits [0-9] .
>
>
> I want to write a script removing only those files that contain both
> alphabets and digits. One trick might me to use only property (2) to
> remove files with property (1). (this is like grep -v grep )
>
> Please me /

You could use ls -1 to produce a list of filenames, parse this list through
an awk / perl / sed script or a compiled program to produce another list
containing all the file names to delete and use this list as an input for a
shell script doing the deleting operation with a while read ... < that_list
loop.

Axel
  Réponse avec citation
Vieux 10/09/2007, 00h56   #7
William James
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rm files not matching (tricky) !!

On Sep 9, 5:31 am, Stephane CHAZELAS <this.addr...@is.invalid> wrote:
> 2007-09-09, 12:04(+02), Michal Nazarewicz:
> [...]> As far as I understand your question you cannot do such a thing. As far
> > as I know you cannot test if file name contains only digits

>
> [...]
>
> Not with standard globbing, but with ksh globbing (enabled in
> bash with shopt -s extglob and in zsh with setopt kshglob) or
> with zsh globbing (with extendedglob), you can.
>
> ksh: *([0-9])
> zsh: [0-9]# or <-> (<x-y> being positive decimal integers
> ranging from x to y).


ruby -e 'puts Dir["*"].grep(/^\d+$/)'


  Réponse avec citation
Vieux 13/09/2007, 11h56   #8
Michal Nazarewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: rm files not matching (tricky) !!

> On Sat, 08 Sep 2007 20:24:38 -0700, onkar wrote:
>> I have large number of files in a directory with filenames
>> containing
>>
>> (1) numbers [0-9] and alphabets [a-z] (for e.g., one file name
>> might be 1232344saasderv while others might have name awer345345ds
>> and sdfs12312 and 1a4d56a345d3 .. any combination ) ,
>>
>> and
>>
>> (2) some files containing only digits [0-9] .
>>
>>
>> I want to write a script removing only those files that contain both
>> alphabets and digits. One trick might me to use only property (2) to
>> remove files with property (1). (this is like grep -v grep )


Axel Schlicht <meiwakudome@email.de> writes:
> You could use ls -1 to produce a list of filenames, parse this list through
> an awk / perl / sed script or a compiled program to produce another list
> containing all the file names to delete and use this list as an input for a
> shell script doing the deleting operation with a while read ... < that_list
> loop.


That brings a problem of new lines in file names.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 08h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17909 seconds with 16 queries