Interval merging


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Interval merging

I do have a file (file1) with a specified interval of 500 counts

file1
Code:
0	500
500	1000
1000	1500
1500	2000
2000	2500
2500	3000
3000	3500
3500	4000
4000	4500
4500	5000
5000	5500
5500	6000
6000	6500
6500	7000
7000	7500
7500	8000
8000	8500
8500	9000
9000	9500
9500	10000
...
.....

I have another file (file2) with 3 columns, where the interval between columns 1 and 2 is 100

Code:
550 600 7
1100	1200	  8
2100 2200 5
3500 3600 8
9600 9700 7

I want to merge file1 and file2 , so that the resultant file (file3) will be merged within the interval of file 1.

File3
Code:
500	1000 550 600 7
1000	1500 1100	1200	  8
2000	2500 2100 2200 5
3500	4000 3500 3600 8
9000	9500 9600 9700 7

It would be great if I could get some help in this interval matching problem either using awk or sed. The real files have 1000's of lines.
# 2  
Code:
awk 'NR == FNR {a[FNR] = $0; x=FNR; next}
  {b[FNR] = $0; y=FNR; next}
  END {m = (x > y) ? x : y;
    for(i = 1; i <= m; i++) print a[i], b[i]}' file1 file2 > file3

# 3  
Another awk approach:
Code:
awk '
        NR == FNR {
                A[++c] = $1 OFS $2 OFS $3
                next
        }
        {
                for ( i = 1; i <= c; i++ )
                {
                        n = split ( A[i], T )
                        if ( T[1] >= $1 && T[2] <= $2 )
                                print $0, A[i]
                }
        }
' OFS='\t' file2 file1

This User Gave Thanks to Yoda For This Post:
# 4  
Does file1 have anything in it other than the ranges? (If not, it isn't needed for this project.)

Can there be more than 1 line in file2 that fits into the same 500 count interval? If there are two input lines in the same interval, are two output lines produced for that interval or are the two lines from file2 merged onto a single line in file3? (If they are merged, is there any limit to the number of lines that should be merged into a single output line?)

Is file2 always sorted by increasing numeric value in the 1st and 2nd fields (as in your example)?
--------------
Update...
Can we assume that the last line of your sample output should have been:
Code:
9500	10000 9600 9700 7

rather than:
Code:
9000	9500 9600 9700 7


Last edited by Don Cragun; 04-30-2014 at 08:17 PM..
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #256
Difficulty: Easy
MILNET gave birth to NIPRNet, which was used to transmit sensitive but unclassified data between internal users and also provide those users with a gateway to the commercial Internet.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Repetition in a particular interval

Suppose I have a word which is repeating in a string continuously. I have a set of intervals. Then how do I find the number occurrences of that word in those intervals and their location of occurrences. For example - Suppose there is a huge string anfie.......sirn of 10000 letters. Now the word... (2 Replies)
Discussion started by: ANKIT ROY
2 Replies

2. Shell Programming and Scripting

While loop in certain interval

Hi Gurus, I have a script which loops a directory to find the file. right now, I want to add time interval which means every 15 minutes it checks the dir once. if all files are found, then exit with 0. if there are some files missing at 6:00am the process exit with 20. the script start at 7:00pm... (15 Replies)
Discussion started by: ken6503
15 Replies

3. Shell Programming and Scripting

counts for every 1000 interval

Hi, I have a file with 4 million rows. Each row has certain number ranging between 1 to 30733090. What I want is to count the rows between each 1000 intervals. 1-1000 4000 1001-2000 2469 ... ... ... ... last 1000 interval Thanks, (7 Replies)
Discussion started by: Diya123
7 Replies

4. Shell Programming and Scripting

Copy files with interval

Hi everybody I am not familiar with UNIX. I have a problem. There is a directory which contains Run1000.dat Run1001.dat Run1002.dat Run1003.dat . . . Run6000.dat I want to copy files from “Run2000.dat” to “Run4060.dat” with interval 20 to another directory. It means I want to copy... (2 Replies)
Discussion started by: bineshb
2 Replies

5. Solaris

NTP polling interval

Is it possible to change the ntp poll manually. I notice that ntp poll is changing autimatically. (1 Reply)
Discussion started by: ningy
1 Replies

6. Programming

Interval year to month

Hi, I'm working on a Informix4gl module. I'm just trying to find out any built-in function to fetch only the year/month from an INTERVAL YEAR TO MONTH data value. Please let me know, if there are any functions to do this. If not, let me know for any alternative solutions to attain this. ... (5 Replies)
Discussion started by: dvah
5 Replies

7. Shell Programming and Scripting

compare the interval of 2 numbers of input2with interval of several numbers of input1

Help plz Does any one have any idea how to compare interval ranges of 2 files. finding 1-4 (1,2,3,4) of input2 in input1 of same key "a" values (5-10, 30-40, 45-60, 80-90, 100-120 ). Obviously 1-4 is not one of the range with in input1 a. so it should give out of range. finding 30-33(31,32,33)... (1 Reply)
Discussion started by: repinementer
1 Replies

8. UNIX for Advanced & Expert Users

Summation of the column value in an interval.

Hey, I have a file with following data.. and I want to get the summation of 10th column. And this happens in every 10 seconds for a minute max. 242001 A mqsitst 794864 1249516 0 60 20 293050400 77016 * 02:05:33 - 0:04 DataFlowEngine EAITIBR2_BROKER... (6 Replies)
Discussion started by: varungupta
6 Replies

9. OS X (Apple)

interval in a launchd script

Hi, i'm planning a to automate my backup with a rsync user deamon in my launchd and let that run every 2 hours. my server contains a lot of video data which can expand rapidly , up to 1TB, in an interval of 2 hours. now i'm wondering what will happen when a rsync is running longer then the... (1 Reply)
Discussion started by: wessel
1 Replies

10. Shell Programming and Scripting

Need Help for interval date

Hello, I need help about a shell script I have a text file with this fields: 2009/01/19 09:33:35: --> ---ORA-28817: PL/SQL function returned an error. 2009/01/19 09:33:35: --> ---ORA-28817: PL/SQL function returned an error. 2009/01/19 09:33:35: --> ---ORA-28817: PL/SQL function returned an... (4 Replies)
Discussion started by: giofai
4 Replies

Featured Tech Videos