Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Highlighted
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