combine lines from two files based on an if statement

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting combine lines from two files based on an if statement
# 1  
Old 10-03-2011
combine lines from two files based on an if statement

I'm rather new to programming, and am attempting to combine lines from 2 files in a way that is way beyond my expertise - any help would be appreciated!

I need to take a file (file1) and add columns to it from another file (file2). However, a line from file2 should only be added to a given line in file1 if the number in file1column2 is between the numbers in file2column4 and file2column5. Therefore, I don't think I can use paste or join, and I can't figure out how to combine awk with getline or some if statement to get this to work. Any insight would be appreciated! Thanks so much!

chr2L    22975381    -1.6723247    chr2L    22975381    -1.8917731    0.219448    0
chr2L    22975421    -1.526086    chr2L    22975421    -1.8669024    0.340816    0    
chr2L    22975458    -1.7168827    chr2L    22975458    -1.9361134    0.219231    0

2L    gadfly    exon    15748693    15748925    .    -    
2L    gadfly    exon    15748515    15748637    .    -    
2L    gadfly    exon    15746596    15748455    .    -

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-03-2011 at 06:04 PM.. Reason: code tags, please!
# 2  
Old 10-03-2011
Based on your input, what is your expected output?
# 3  
Old 10-03-2011
customize this based on your needs:
> combined.txt
for i in $(cat file1);do
    for j in $(cat file2);do
        if [[ `echo $i |awk '{print $2}'` -gt `echo $j |awk '{print $4}'` && `echo $i |awk '{print $2}'` -lt `echo $j |awk '{print $5}'` ]];then
            result+="`echo $j|awk '{print $2}'`,"
    result=`echo $result|sed 's/.$//'`
    echo $i $result >> combined.txt

this will read file1 and file2 , compare them as what you said , and output combined result in combined.txt file.
# 4  
Old 10-03-2011
a bit ugly, but:
nawk '
  FNR==NR{f2s[FNR]=$4; f2f[FNR]=$5; f2[$4,$5]=$0;next}
     for(i=1;i in f2s;i++)
       if ($2>=f2s[i] && $2<=f2f[i])
          print $0, f2[f2s[i], f2f[i]]
  }' file2 file1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine and complete multiple CSV files based on 1 parameter

I have to create a new CSV file based on the value listed on the 3rd column from different CSV files. This is what I need: 1. I should substitute the first column from each file, excluding the headers, with the file name InputXX. 2. Then, I need to look for rows with 0 on the third column in... (7 Replies)
Discussion started by: Xterra
7 Replies

2. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

3. Shell Programming and Scripting

Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited 1,2,3,4 1 1 1,2,3,4 1,2 2 2,3,4 My required output must have only 4 columns with comma delimited 1,2,3,4 111,2,3,4 1,222,3,4 I have tried many awk command using ORS="" but couldnt progress (10 Replies)
Discussion started by: mdkm
10 Replies

4. Shell Programming and Scripting

Combine 3 files based on a pattern

HI, I have 3 files that contain the following information (sql output from Oracle database stored in a txt file): File1.txt : alter table "SYS"."INT_COST_PRICE" enable row movement; alter table "SYS"."INT_SOH" enable row movement; alter table "SYSMAN"."XX_ACI_SKURTP" enable row movement;... (6 Replies)
Discussion started by: rparavastu
6 Replies

5. Shell Programming and Scripting

Combine multiple lines in file based on specific field

Hi, I have an issue to combine multiple lines of a file. I have records as below. Fields are delimited by TAB. Each lines are ending with a new line char (\n) Input -------- ABC 123456 abcde 987 890456 7890 xyz ght gtuv ABC 5tyin 1234 789 ghty kuio ABC ghty jind 1234 678 ght ... (8 Replies)
Discussion started by: ratheesh2011
8 Replies

6. Shell Programming and Scripting

Combine the lines from separate text files

Hi All, I have three separate text files which has only one line and i want to combine these lines in one text file which will have three lines. cat file1.txt abc cat file2.txt 1265 6589 1367 cat file3.txt 0.98 0.36 0.5 So, I want to see these three lines in the... (9 Replies)
Discussion started by: senayasma
9 Replies

7. Shell Programming and Scripting

How to combine two files based on fields?

I have two files which are as follows: File 1: 1 abc 250 2 pqr 300 3 xyz 100 File 2: 1 abc 230 2 pqr 700 3 xyz 500 Now I need output File, File 3as: S.No Name Count1 Count2 1 abc 250 230 2 pqr 300 700 3 xyz 100 500 NOTE: (13 Replies)
Discussion started by: karumudi7
13 Replies

8. Shell Programming and Scripting

Combine the lines based on particular pattern

Hi, I've a weird problem to be solved. Assume i have a file like this: 1. <timestamp> UID: 12345 <junk> DevID: V123 2. <timestamp>DevID: V123 <junk> DuID: VP 3. ... 4. .... 5. <timestamp> UID: 789 <junk> DevID: S456 6. <timestamp>DevID: S456 <junk> DuID: VP.... 7. ..... Say if i... (3 Replies)
Discussion started by: VenkataPrasad
3 Replies

9. Shell Programming and Scripting

how to combine 2 lines in same files based on any text

hi, I want to combine two lines in same file. If the line ends with '&' it should belongs to previous line only Here i am writing example. Ex1: line 1 : return abcdefgh& line 2 : ijklmnopqr& line 3 : stuvw& line 4 : xyz output should be line 1: return abcdefghijklmnopqrstuvwxyz ... (11 Replies)
Discussion started by: spc432
11 Replies

10. Shell Programming and Scripting

How can I get an if statement to execute based on number of lines in a file?

I need to have an if statement in a script to run if there are certain processes running. Easiest way I can see to do this is to run a ps and grep the results based on what I am looking for: $ ps -ef | grep wtrs --- webtrend 5046 1 0 May 12 ? 0:28 /webtrends/versions/6.1/wtrs_ui... (6 Replies)
Discussion started by: LordJezo
6 Replies
Login or Register to Ask a Question