|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 / |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 .. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
>> 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-- |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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+$/)' |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
> 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-- |
|
![]() |
| Outils de la discussion | |
|
|