Need help with the logic for ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with the logic for ksh script
# 1  
Old 08-03-2017
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

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 08-03-2017 at 02:46 PM.. Reason: Added CODE tags.
# 2  
Old 08-03-2017
Is that line in a file? A shell variable? Try

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

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

Those $'\n' are bashisms; 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) ...

Last edited by RudiC; 08-03-2017 at 02:52 PM..
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-03-2017
In case we're dealing with shell variables, try

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

This User Gave Thanks to RudiC For This Post:
# 4  
Old 08-03-2017
Quote:
Originally Posted by desant
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

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!
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.
# 5  
Old 08-04-2017
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
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (1 Reply)
Discussion started by: desant
1 Replies

2. Shell Programming and Scripting

import var and function from ksh script to another ksh script

Ih all, i have multiples ksh scripts for crontab's unix jobs they all have same variables declarations and some similar functions i would have a only single script file to declare my variables, like: var1= "aaa" var2= "bbb" var3= "ccc" ... function ab { ...} function bc { ... }... (2 Replies)
Discussion started by: wolfhurt
2 Replies
Login or Register to Ask a Question