LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2010, 06:43 AM   #1
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Rep: Reputation: 0
Exclamation help with file reading/compare in bash scripts


Hello all

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).

please help
thank you!!
 
Old 12-09-2010, 08:19 AM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
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

array1 = {2,3,4,5}
array2 = {3,6,1,4}
array3 = {5,9,2,7}

Then you can loop through the comparisons by array position.
 
Old 12-09-2010, 08:35 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I think I understand the comparing part of the question, but I don't seem to see anywhere what the expected output is?
 
Old 12-09-2010, 04:26 PM   #4
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Original Poster
Rep: Reputation: 0
hello

thanks for your reply! my expected output would be to find wich values from file.a, belong inbetween wich group from file.b.
example.

3 5,----> 3,4,5
6 9,----> null
1 2,----> 2
4 7,----> 4,5

(All this i wish it to be put into a text file showing which values are inbetween which range)

my version so fare just compares everything so it places all the values inbetween every possible combination :S

Last edited by rossk; 12-09-2010 at 04:33 PM.
 
Old 12-09-2010, 04:30 PM   #5
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Original Poster
Rep: Reputation: 0
also to thesnow

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++...)

Last edited by rossk; 12-09-2010 at 04:35 PM.
 
Old 12-09-2010, 09:15 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So your output is not the same as what you originally asked for as now the numbers from file.a are between AND inclusive (quite a different thing)

So maybe:
Code:
awk 'FNR==NR{csize=split($0,compare,",");next}{for(x=1;x<=csize;x++)if(compare[x] >= $1 && compare[x] <= $2)out=out" "compare[x];if(out)print out;else print "null";out=""}' file.a file.b
 
Old 12-09-2010, 10:10 PM   #7
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Original Poster
Rep: Reputation: 0
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?
 
Old 12-10-2010, 12:40 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
it has 2 apostrophes on each side of the code, are they supposed to be there?
Yes otherwise awk will get upset. You can run mine straight from the command line.

I would stress again that you have asked for inclusive in post #4 so even your code will need to change to >= and <= NOT > and <
 
Old 12-10-2010, 07:35 PM   #9
rossk
LQ Newbie
 
Registered: Dec 2010
Posts: 12

Original Poster
Rep: Reputation: 0
ahh yes i see that lol, its still in beta testing hahaha,
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to compare numbers in a txt file leopard86 Programming 6 09-11-2012 12:10 AM
Reading From Text File In Bash ke3r4 Programming 16 11-17-2010 08:52 AM
need help with bash script to compare file sizes rimvydazas Programming 3 04-03-2008 06:18 AM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM
bash; reading values from a file km4hr Programming 16 07-28-2005 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration