[SOLVED] help with file reading/compare in bash scripts
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i am just starting to learn how to program in bash, an i have a problem, i am trying compare values inbetween 2 values (from another file), so far my solution is to make a nested for loop, but that causes it to compare every value. Here is a visulization of what i want
file.a 2,3,4,5
file.b
3 5
6 9
1 2
4 7
i want the values 2, 3, 4, 5 from file.a to be compared inbetween values 3 5, 6 9,1 2, 4 7 from file.b
i have come up with this solution
(var1 is the value im comparing, var2 is the less value, var 3 is the greater value)
for i in $var1
do
for k in $var2
do
for l in $var 3
do
if [$i -gt $k ] && [$i -lt $l]
then
blah blah blah
done done done
my problem with the above code is it compares EVERYINNG, not the values inbetween what i want (which is 3 5, 6 9 etc).
There is probably an easier way to do this, but I would create three arrays: one with the contents of file.a, the second with the contents of the first column in file.b, and the third with the contents of the second column in file.b. The array contents would be
your solution seems possible but not sure how to loop through an array based on its array position, (would it be like java, just grab the index say array[1], array[2]?? or place it in a for loop to iterate ...array[i] i++...)
I also came up with this piece of code while i was playing around. this code seems to work only it duplicates the values about 3 times!!! argh
for (( i = 0; i < ${#array[@]} ; i++ ))
do
for (( j = 0 ; j < ${#array[@]} ; j++ ))
do
if [ "${array[j]}" -gt "${array2[i]}" ] && [ "${array[j]}" -lt "${array3[i]}" ]
then
echo etc etc
fi
done
done
grail, im gonna try your solution, but i noticed that it has 2 apostrophes on each side of the code, are they supposed to be there?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.