LinuxQuestions.org
Visit Jeremy's Blog.
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 03-02-2011, 02:08 AM   #1
astroumut
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Rep: Reputation: 0
Matching two tables of non-matching sizes


Hi All,

I have two table files with x (1st column) ,y (2nd column) coordinates and intensity (3rd column).
I need to match these two tables and divide the intensities at the consecutive coordinates on the 3rd column. The problem is the size of the tables are not same and I want to ignore the lines if they are not in one of the other file.

How can I do that in sed, awk or etc?

I would be glad if anyone can help.

Thanks


Here is Table 1:
Code:
-7.500	-30.000	13.006
-7.500	-22.500	37.952
-7.500	-15.000	60.962
-7.500	-7.500	40.922
-7.500	0.000	14.348
-7.500	7.500	6.092
-7.500	15.000	6.077
-7.500	22.500	9.283
-7.500	30.000	6.342
0.000	-30.000	10.859
0.000	-22.500	17.416
0.000	-15.000	39.992
0.000	-7.500	45.955
0.000	0.000	16.917
0.000	7.500	7.004
0.000	15.000	10.892
0.000	22.500	11.020
0.000	30.000	10.698
Here is table 2:
Code:
-7.500	-7.500	63.619
-7.500	0.000	32.029
-7.500	7.500	9.744
0.000	-15.000	92.869
0.000	-7.500	108.460
0.000	0.000	31.545
0.000	7.500	2.784
0.000	15.000	6.779
Desired output:
Code:
-7.500	-7.500	0.643235511
-7.500	0.000	0.447969028
-7.500	7.500	0.625200131
0.000	-15.000	0.430628089
0.000	-7.500	0.423704592
0.000	0.000	0.536281503
0.000	7.500	2.516021266
0.000	15.000	1.606631855
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-02-2011, 08:20 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
The program below will read your input and reproduce your output. Almost. The difficulty is that your output figures are not as accurate as they are precise. My output figures match the results from the same division operations performed by Perl and by my HP-16c calculator. First, my output:
Code:
-7.500  -7.500  0.643235511
-7.500  0.000   0.447969028
-7.500  7.500   0.625205255
0.000   -15.000 0.430628089
0.000   -7.500  0.423704592
0.000   0.000   0.536281503
0.000   7.500   2.515804598
0.000   15.000  1.606726656
And here's the code:
Code:
#!/usr/bin/clisp

(defparameter table-1 "in-1")
(defparameter table-2 "in-2")

(defun d-float (x)
   (read-from-string (concatenate 'string (format nil "~a" x) "d0")))
(defun get-line (the-stream)
   (let ((buffer (read-line the-stream nil)))
      (cond
         (buffer
            (read-from-string (concatenate 'string "(" buffer ")")))
         (t
            nil))))
(defun compare ()
   (cond
      ((or (not item-1) (not item-2))
         nil)
      ((< (car item-1) (car item-2))
         -1)
      ((> (car item-1) (car item-2))
         1)
      ((< (cadr item-1) (cadr item-2))
         -1)
      ((> (cadr item-1) (cadr item-2))
         1)
      (t
         0)))
(defun sync-up ()
   (do
         ()
         ((or (not (compare)) (= (compare) 0))
            (cond
               ((not (compare))
                  nil)
               (t
                  (/ (d-float (caddr item-1))
                     (d-float (caddr item-2))))))
      (cond
         ((< (compare) 0)
            (setq item-1 (get-line stream-1)))
         ((> (compare) 0)
            (setq item-2 (get-line stream-2))))))
(setq stream-1 (open table-1))
(setq stream-2 (open table-2))
(setq item-1 (get-line stream-1))
(setq item-2 (get-line stream-2))
(do
      ()
      ((or (not item-1) (not item-2)))
   (cond
      ((sync-up)
         (format t "~5,3f       ~5,3f   ~11,9f~%"
            (car  item-1)
            (cadr item-2)
            (sync-up))
         (setq item-1 (get-line stream-1)))
      (t
         nil)))
 
2 members found this post helpful.
Old 03-02-2011, 08:59 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
An awk solution:
Code:
BEGIN {
  FS = OFS = "\t"
  OFMT = "%.9f"
}

FNR == NR {
  _[$1$2] = $3
}

FNR < NR {
  if ( $1$2 in _ ) { 
    $3 = _[$1$2]/$3
    print $1, $2, $3
  }
}
Run as:
Code:
awk -f script.awk table1 table2
where the order of the arguments is: first the dividend, second the divisor.
 
1 members found this post helpful.
Old 03-03-2011, 08:05 AM   #4
astroumut
LQ Newbie
 
Registered: Aug 2009
Posts: 10

Original Poster
Rep: Reputation: 0
Thanks a lot, great, they are working very well.

Umut
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 9 06-30-2016 06:30 PM
[SOLVED] Matching Name NetRock Programming 17 09-30-2010 11:01 AM
Perl Script needed to be reversed to output matching, not non-matching 0bfuscated Programming 2 07-20-2010 11:51 AM
pattern matching vinaytp Linux - Newbie 2 10-10-2009 07:06 AM
matching tables ovince Programming 10 08-19-2007 02:18 AM

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

All times are GMT -5. The time now is 08:40 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration