Operating System - HP-UX
1847738 Members
4338 Online
104013 Solutions
New Discussion

file name pattern matching in a script

 
SOLVED
Go to solution
Dermot Beirne
Frequent Advisor

file name pattern matching in a script

Hi,
I have a lot of file names with ascending numeric patterns in their names. e.g. file1234, file1235, file1236, etc.
I am trying to list the files whose names fall within a certain numeric range, e.g. file1234-file1239.
I've tried everything and am sure i am making a stupid omission somewhere, but can't waste any more time searching the web and reading for an answer. Can someone please take me out of my misery!
Thanks.
Dermot.
Happy is harder than money. Anyone who thinks money will make them happy, doesn't have money.
7 REPLIES 7
harry d brown jr
Honored Contributor

Re: file name pattern matching in a script


ls file123[4-9]

# cd /tmp
# touch file1230
# touch file1233
# touch file1234
# touch file1235
# touch file1236
# touch file1238
# touch file1237
# touch file1239
# ls file123[4-9]
file1234 file1235 file1236 file1237 file1238 file1239
# ls file123[5-8]
file1235 file1236 file1237 file1238
#


live free or die
harry


Live Free or Die
Yogeeraj_1
Honored Contributor

Re: file name pattern matching in a script

hi,

this do it:
ls -alR file12[34-39]

or if there are more files, you can also do a:
ls -alR file12[34-39]*

using wild cards * or ?, it becomes easy.

Cheers
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Carlos Fernandez Riera
Honored Contributor
Solution

Re: file name pattern matching in a script

check this:

ll file12* | awk '/file1234/,/file1243/ { print}'
unsupported
harry d brown jr
Honored Contributor

Re: file name pattern matching in a script


Yogeeraj,

# ls file12[34-39]*
file12[34-39]* not found
#

the [34-39] doesn't work because the "range" doesn't work that way. The "range" is a single range. IE a-z works, but not aa-ku.

live free or die
harry
Live Free or Die
Ceesjan van Hattum
Esteemed Contributor

Re: file name pattern matching in a script

The method of Carlos works perfect. First posting of Harry D. Brown looks good, but as Harry indeed telles us, the [xx-yy] does not work. Only one charachter-range is available.

Don't you just love awk.. :)
Ceesjan
harry d brown jr
Honored Contributor

Re: file name pattern matching in a script

With these files

# ls file1*
file1 file1220 file1230 file1234 file1236 file1238 file1244 file1255
file1211 file1225 file1233 file1235 file1237 file1239 file1245

you can display ranges using this syntax, as in listing files that are

file1224-file1227 & file1234-file1237


# ls file12[2-3][4-7]
file1225 file1234 file1235 file1236 file1237
#

if you want all files starting at file1224 through file1237, then this syntax works:

# ls file122[4-9] file123[0-7]
file1225 file1230 file1233 file1234 file1235 file1236 file1237
#

live free or die
harry
Live Free or Die
Dermot Beirne
Frequent Advisor

Re: file name pattern matching in a script

Thanks to all, but especially Carlos. Your awk statement was spot on. The problem with the others was that it did not allow for going from file12301 to file12311 easily.

Many thanks.
Dermot
Happy is harder than money. Anyone who thinks money will make them happy, doesn't have money.