[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: string to integer

From: pk
Subject: Re: string to integer
Date: Tue, 04 May 2010 15:48:49 -0000
User-agent: KNode/4.3.5

Javier Montoya wrote:

> Dear all,
> I have a several directories with *.jpg images. The image files are
> named as ascending numbers and I would like to retrieve the lowest and
> largest image number in each directory. An example of the content of a
> directory is given below:
> /bla/bla/bla/dir1
> -> 00000.jpg
> -> 00001.jpg
> ->
> -> 09001.jpg
> The desired output would be the integer numbers: 0 and 9001
> With the code below, I'm able to retrieve in "seqInterval" array,
> which is the lowest and largest image number as strings.
> seqDir="/bla/bla/bla/dir1"
> seqInterval=($(find $seqDir -name "*.jpg" | sort | sed -n -e '1p;$p' |
> sed -e 's|^.*/||' -e 's/\.[^.]*$//'))
> in our example, I obtain sth. like:
> seqInterval[0] is equal to "00000" and seqInterval[1] is equal to
> "09001"
> Since those are string numbers, I was wondering which is the best way
> to convert those strings into integer numbers (seqInterval[0]=0 and
> seqInterval[1]=9001)?
> I tried the code below, but it doesn't work properly, any suggestions?
> startImg=$(printf '%d' ${seqInterval[0]})
> endImg=$((printf '%d' ${seqInterval[1]}))

In this specific case, try

ls | awk -F. 'NR==1{print $1+0}END{print $1+0}'

I suggest you send these messages to comp.unix.shell, as this group is for 
bash bugs.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]