Re: show files in a numeric range (316 Views)
Reply
Regular Advisor
support_billa
Posts: 183
Registered: ‎06-27-2011
Message 1 of 9 (323 Views)
Accepted Solution

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

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,380
Registered: ‎03-06-2006
Message 2 of 9 (317 Views)

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

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 9 (316 Views)

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

Please use plain text.
Regular Advisor
support_billa
Posts: 183
Registered: ‎06-27-2011
Message 4 of 9 (312 Views)

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

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 9 (309 Views)

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

Please use plain text.
Regular Advisor
support_billa
Posts: 183
Registered: ‎06-27-2011
Message 6 of 9 (288 Views)

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

 

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,380
Registered: ‎03-06-2006
Message 7 of 9 (282 Views)

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?

Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 9 (280 Views)

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

Please use plain text.
Honored Contributor
H.Merijn Brand (procura
Posts: 6,185
Registered: ‎10-13-1997
Message 9 of 9 (262 Views)

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
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation