1

I have been trying to get grep to work to find the intersection between 2 lines:

grep -Fx -f line1.txt line2.txt

Input file contents:

line1.txt:

44.5   -125.0    
44.0   -124.5    
43.0   -124.3    
42.0   -124.0

line2.txt:

43.0 -128.0    
43.1 -127.0    
43.2 -126.0    
43.3 -125.0    
43.4 -124.0    
43.5 -123.0    
43.6 -122.0    
43.7 -121.0

The problem is the 2 lines do not have a row that is exactly the same.

Does anyone have a simple way to find the intersecting point?

The following image shows the 2 lines and approx intersection point: enter image description here

I was hoping to have a command find the intersection point for me. grep probably cannot do this? Also, tried GMT commands, but could not get that to work either. Any advice?

I would settle for a command that would find and write the line2.txt points that are the closest to the intersection:

enter image description here

  • 1
    What output exactly do you expect for given files? – jimmij Dec 30 '17 at 0:18
  • 2
    Given that the two files don't have any complete lines in common, could you clarify what you mean by "intersecting point"? Post the output you want. Also, you're probably looking for join (but beware that its input must be alphabetically sorted, not numerically sorted). – Wildcard Dec 30 '17 at 0:19
  • 2
    Is this supposed to be some sort of geometrical intersection? – Jeff Schaller Dec 30 '17 at 0:27
  • 1
    @JeffSchaller that was exactly my first though, but that would be well beyond grep capabilities. – jimmij Dec 30 '17 at 0:39
  • I’m voting to close this question as unclear because you haven’t adequately explained what you’re talking about nor given the desired output for the given input.  But I believe I sort-of understand what you want, and it’s a math thing, and grep doesn’t do math.  You might be able to do what you want in awk. – G-Man Dec 30 '17 at 3:36
1

Grep is flat out the wrong tool for this! And sed is not much better as you need to do some math. Perl is be a candidate, but the bottom line is that you need to do line intersection testing. Here are a couple links to get you started. Good thing unix is a great platform for writing programs, because you need to write a program.

https://en.wikipedia.org/wiki/Line_segment_intersectionhttps://en.wikipedia.org/wiki/Line_segment_intersection

https://stackoverflow.com/questions/563198/whats-the-most-efficent-way-to-calculate-where-two-line-segments-intersect

  • I thought this might be the case. I can write this script in Matlab, but the only program language I can use on the computer this needs to be done on is Fortran. Now trying to learn how to write it =-). Thanks all! – MattS Jan 2 '18 at 18:04
0

I can guess that you thinking about something like this:

( cat line2.txt; cat line1.txt | sed 's/  */ xxxx /' ) | sort -n | awk '$2 == "xxxx" {f2=$3} $2 != "xxxx" {f1 = $2} {print $1" "f1" "f2}'

output would be:

42.0  -124.0
43.0 128.0 -124.0
43.0 128.0 -124.3
43.1 127.0 -124.3
43.2 126.0 -124.3
43.3 125.0 -124.3
43.4 124.0 -124.3
43.5 123.0 -124.3
43.6 122.0 -124.3
43.7 121.0 -124.3
44.0 121.0 -124.5
44.5 121.0 -125.0
  • with the clarification of an edit of the question we can now see that your answer does not do line segment intersection which is what the op was actually looking for. – hildred Jan 1 '18 at 16:53
  • Yeah, after clarification it is clear that my guess was wrong. Would be nice to remove the answer but I am not sure how. – gena2x Jan 8 '18 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.