Operating System - HP-UX
1748080 Members
5318 Online
108758 Solutions
New Discussion

Re: show files in a numeric range

 
SOLVED
Go to solution
support_billa
Valued Contributor

show files in a numeric range

In directories I have hundreds of files like:

file0001.lst
file0002.lst
.
.
file0999.lst

i want to show files in a index range, example 30 til 60 .
input : index1
input : index2

 

my idea:


index1=$1
index2=$2

for file in $( ls file* )
do
 index=$( basename ${file} .lst |sed "s|[a-z]||g" )
 if [ ${index} -ge ${index1} -a ${index} -le ${index2} ]
 then
   ls -al $file
 fi
done

8 REPLIES 8
Dennis Handly
Acclaimed Contributor

Re: show files in a numeric range

If your format is always: file####.lst:

#!/usr/bin/ksh

# List files by range

if [ $# -ne 2 ]; then

   echo "Usage: $0 first last" 1>&2

   exit 1

fi

typeset -i first=$1 second=$2

typeset -Z4 subnum

ls $(

while (( first <= second )); do

   subnum=$first

   echo "file$subnum.lst"

   (( first += 1 ))

done) 2> /dev/null  # ignore missing files

James R. Ferguson
Acclaimed Contributor

Re: show files in a numeric range

Hi:

 

One way:

 

# perl -nle '($n)=m{\D+(\d+)};print if $n>=30 && $n<=60' file

...prints the names matching the criteria.

 

# perl -nle '($n)=m{\D+(\d+)};system(qq(ls -l $_)) if $n>=30 && $n<=60' file

...runs 'ls -l' on the matches.

 

Regards!

 

...JRF...

support_billa
Valued Contributor

Re: show files in a numeric range

hello,

 

both answers are perfect, shell i understand , perl it isn't so easy, but i can extend to use shell variables

 

perl -nle '($n)=m{\D+(\d+)};print if $n>=30 && $n<=60' file

 

my version for using in a shell, hope right ?

ls file* | \
first=98 second=105 perl -nle '($n)=m{\D+(\d+)};print if $n>=$ENV{"first"} && $n<=$ENV{"second"}' 

 

regards

James R. Ferguson
Acclaimed Contributor
Solution

Re: show files in a numeric range

Hi:

 

Your Perl modification was very well done!  You can use barewords for the hash keys, though:

 

# ls file* | first=30 second=60 perl -nle '($n)=m{\D+(\d+)};print if $n>=$ENV{first} && $n<=$ENV{second}'

Regards!

 

...JRF...

support_billa
Valued Contributor

Re: show files in a numeric range

hello,

 

last question : when i try:

 

ls /tmp/20111010/file/file* | \
first=95 second=105 perl -nle '($n)=m{\D+(\d+)};print if $n>=$ENV{first} && $n<=$ENV{second}'

 

it shows nothing , print $n shows 20111010 .

 

ls /tmp/file/file* | \
first=95 second=105 perl -nle '($n)=m{\D+(\d+)};print if $n>=$ENV{first} && $n<=$ENV{second}'

 

is ok

 

regards

 

Dennis Handly
Acclaimed Contributor

Re: show files in a numeric range

>when I try:  ls /tmp/20111010/file/file* | \

>it shows nothing, print $n shows 20111010 .

 

Most likely it takes the first string of digits in your directory path and not the last in your filename?

James R. Ferguson
Acclaimed Contributor

Re: show files in a numeric range


@support_billa wrote:

 

last question : when i try:

 

ls /tmp/20111010/file/file* | \
first=95 second=105 perl -nle '($n)=m{\D+(\d+)};print if $n>=$ENV{first} && $n<=$ENV{second}'

 

it shows nothing , print $n shows 20111010 .

 


Hi:

 

What's different about '/tmp/file/file' versus ' /tmp/20111010/file/file' ?

 

That is, show us what your 'ls' of each returns.

 

Regards!

 

...JRF...

H.Merijn Brand (procura
Honored Contributor

Re: show files in a numeric range

The number test should be done on the file name only if I read this thread correctly, not on the directory name. I'd use File::Find;

$ perl -MFile::Find -wle'find(sub{/(\d+)/&&$1>=95&&$1<=105&&print},"/tmp/20111010/file")'

 but you might find that too terse

 

Enjoy, Have FUN! H.Merijn