Re: show files in a numeric range (614 Views)
Reply
Valued Contributor
Posts: 192
Registered: ‎06-27-2011
Message 1 of 9 (675 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

Acclaimed Contributor
Posts: 26,099
Registered: ‎03-06-2006
Message 2 of 9 (669 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

Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 9 (668 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...

Valued Contributor
Posts: 192
Registered: ‎06-27-2011
Message 4 of 9 (664 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

Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 9 (661 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...

Valued Contributor
Posts: 192
Registered: ‎06-27-2011
Message 6 of 9 (640 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

 

Acclaimed Contributor
Posts: 26,099
Registered: ‎03-06-2006
Message 7 of 9 (634 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?

Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 9 (632 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...

Highlighted
Honored Contributor
Posts: 6,205
Registered: ‎10-13-1997
Message 9 of 9 (614 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
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.