Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

show files in a numeric range

SOLVED
Go to Solution
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

1 ACCEPTED SOLUTION
Acclaimed Contributor Acclaimed Contributor

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...

8 REPLIES
Acclaimed Contributor Acclaimed Contributor

Re: show files in a numeric range

[ Edited ]

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

Highlighted
Acclaimed Contributor 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...

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

Acclaimed Contributor Acclaimed Contributor

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...

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

 

Acclaimed Contributor Acclaimed Contributor

Re: show files in a numeric range

[ Edited ]

>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?

Acclaimed Contributor Acclaimed Contributor

Re: show files in a numeric range

[ Edited ]

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...

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