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

5
  • 2
    What output exactly do you expect for given files?
    – jimmij
    Commented Dec 30, 2017 at 0:18
  • 3
    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
    Commented Dec 30, 2017 at 0:19
  • 2
    Is this supposed to be some sort of geometrical intersection?
    – Jeff Schaller
    Commented Dec 30, 2017 at 0:27
  • 1
    @JeffSchaller that was exactly my first though, but that would be well beyond grep capabilities.
    – jimmij
    Commented Dec 30, 2017 at 0:39
  • 1
    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. Commented Dec 30, 2017 at 3:36

2 Answers 2

2

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_intersection

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

1
  • 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
    Commented Jan 2, 2018 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
3
  • 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
    Commented Jan 1, 2018 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
    Commented Jan 8, 2018 at 16:06
  • 1
    @gena2x You can flag it as "in need of moderator intervention".
    – Sparhawk
    Commented Sep 17, 2019 at 2:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .