i have a list of numbers, ever changing that i need to to extract the
first free number in the series
example
1,2,3,67,100,10000 first free is 4
(not counting zero)
i hope to use /bin/sh on freebsd.
any and all help and is very much appreciated
thanks
jon
how/where is the list of #'s stored?
list="1,2,3,67,100,10000"
awk 'BEGIN{RS=","}{a[$0]=1}END{
for (i=1; a[i]; i++); print i}' << EOF
$list
EOF
--
Stéphane
[...]
> i have a list of numbers, ever changing that i need to to extract the
> first free number in the series
>
> example
>
> 1,2,3,67,100,10000 first free is 4
> (not counting zero)
[...]
awk '
{
if ( $1 > last + 1 ) { print last + 1; exit (0); }
last = $1
}
' "$@"
Heiner
--
___ _
/ __| |_ _____ _____ _ _ Heiner STEVEN <heiner...@nexgo.de>
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/
Better than mine. However it fails to return 4 for "1,2,3"
awk 'BEGIN{RS=","} $1 != NR {exit} {n++} END {print n+1}' << EOF
1,2,3
EOF
--
Stéphane
One way you can do it is to transpose all the COMMA characters
into NEWLINEs so that each number is on a seperate line, then
feed it into 'awk',
NEXTV=$( tr , '\12' <file.in |awk 'NR!=$1{exit(x++)}END{print NR+1-x}' )
where, file.in = the name of the input data file
The first value free is the line number of the first line
which is not equal to the value on that line itself, or
the next line otherwise (if there are no values in the list
NR=0 in the END block )
If the list is in a string then you can just use the same
construct with a little modifcation
NEXTV=$( tr , '\12' <<: |awk 'NR!=$1{exit(x++)}END{print NR+1-x}'
$LISTV
:
)
This is all assuming that your list is in numerical order of course,
if it's not then simply add a 'sort -n' in the pipeline after 'tr'
bestwishes
laura
--
alt.fan.madonna |news, interviews, discussion, writings
|chat, exchange merchandise, meet fans....
|Get into the groove baby you've got to... check us out!
What's the problem with RS?
Yeah, right! :-)
>
> i have a list of numbers, ever changing that i need to to extract the
> first free number in the series
>
> example
>
> 1,2,3,67,100,10000 first free is 4
> (not counting zero)
I think Ian Springer already asked a crucial question:
Where is this list stored?
Besides that, I have a few more to ask:
1) How is the 'ever changing' list going to be accessed
so that the next number 'extracted' (you mean 'determined'
I believe) from the list occurs when the list is not
changing. This is called 'exclusive' access.
2) Can we assume the list is stored in a file?
3) Are the 'comma' delimiters there for illustration or
will they really exist in (whereever the list is stored).
And so on ...
>
> i hope to use /bin/sh on freebsd.
Good for you! But the design is more important than the
implementation language/platform.
> any and all help and is very much appreciated
>
> thanks
> jon
HTH
Tim - PITA
I did the same for a script function designed to set alarms by number --
ten in total. The extract of the code, which uses no external SW tools and
therefore is very efficient, is:
let alrmno=0 p_alrmno=0
[[ -f ~/.alarms ]] || : >|~/.alarms
set -s -A _inst -- $(
while read -r x1 x2
do print -r -- $x1
done <~/.alarms
) 9
# find lowest alarm number
for alrmno in ${_inst[*]}
do if ((alrmno!=p_alrmno+1))
then alrmno=$p_alrmno+1 break
else let p_alrmno=alrmno
fi
done
((alrmno>9)) &&
{ print -ru2 $0: too many alarms
alrmno=0 return 1
}
((!alrmno)) && alrmno=1
print -ru2 [ $0: setting alarm number $alrmno ]
=Brian
If the list wasn't sorted then you can make the simple adjustment of
inserting a 'sort -n' and it wasn't specified whether the list was
sorted or not.
Also I was considering the possibility of a file of such lines;
1,2,3,4,5
6,8,10,11,12
byefornow
this was the best solution in my case, thanks all who helped
jon