Need help with the logic for ksh script

Hi guys
I am having hard time to figure out the logic for the following problem:

I have a line of sorted numbers , for example 19 34 44 49 64
I am trying to find the way to determine which two numbers are in between the number I have specified.

for example if 34 is specified 19 and 44 should be returned.

Thanks a lot for any help and ideas

Is that line in a file? A shell variable? Try

tr ' ' $'\n' < file | grep -C1 34
19
34
44

or

echo $X | tr ' ' $'\n' | grep -C1 34
19
34
44

Those $'\n' are bash isms; replace with ksh 's equivalent for line feeds...

If you really really really insist on the 34 being eliminated, append a | grep -v 34 to the pipe (but then you should make up your mind to write a small awk script) ...

1 Like

In case we're dealing with shell variables, try

X="19 34 44 49 64"
PT=34
LX=${X%% ${PT}*}
RX=${X##*${PT} }
echo ${LX##* }, ${RX%% *}
19, 44
1 Like

This is very vague! In addition to the question of whether the line is in a variable or in a file, there is also the question "How is the number you have specified found?". Is it a command line parameter? Is it a constant? Is it a variable?

And, is the number you specify always a value that is in the line? The code RudiC suggested assumes that it will be. (And, since that was true in your example, that is a reasonable assumption.) But the logic will need to be different if that assumption is not always valid.

As always, it also helps us avoid wasting time making suggestions that won't work if you tell us what operating system (including version number) and shell (including version number) you're using. The grep -C option is a non-standard feature that is available on some operating systems, but not all. The $'\n' expansion is recognized by 1993 and later versions of the Korn shell, but not by the 1988 and earlier versions.

Hi.

Comments:

Given the "line" 19 34 44 49 64

1) "34" is also between 19 and 64, and between 19 and 49. Perhaps you meant "in between" in the sense of closest to

2) "35" is also between 19 and 44 in an arithmetic sense.

So did you mean by position or arithmetically? That's just a rhetorical question, trying to urge you to be more specific in your statements of requirements.

Best wishes ... cheers, drl