Add values < or eq to 1000


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add values < or eq to 1000
# 8  
Old 11-16-2009
repinementer, you have 150++ posts, i am sure you are already competent enough to start showing some of your own code. what have you tried? search back your previous posts and see how awk is used to solve some of your other similar problems.
# 9  
Old 11-16-2009
sorry for the trouble

Here is the detailed input file with explanation in output(bold)
input1
Code:
x1    10
x1    100
x2    1000
x2    10000
x3    989
x4    345
x10    8767736477736
xx    234
xy    387889999

input2
Code:
x1    1010
x1    1100
x1    900
x1    800
x1    10000
x2    2000
x2    3000
x3    1989
x3    2000
x4    1345
x4    1500
x10    8767736478730
x10    10000000
xx    1234
xy    387888999

output
Code:
x1    10    x1    800    [x1  10+/-1000=1010 or -990 i.e all the values bw 1010 and -990 with a similar key x1 those are 800,900,1010 ]
x1    10    x1    900
x1    10    x1    1010    [x1  100+/-1000=1100 or -900 i.e all the values bw 1100 and -900 with a similar key x1 those are 800,900,1010,1100 ]
x1    100    x1    1100
x1    100    x1    800
x1    100    x1    900
x1    100    x1    1010
x2    1000    x2    2000       [x2  1000+/-1000=2000 or 0 i.e all the values bw 2000 and 0 with a similar key x2 those are 2000 ]
x2    10000    NULL    NULL                [Have none]
x3    989    x3    1989         [x3  989+/-1000=1989 or -11 i.e all the values bw 1989 and -11 with a similar key x3 those are 1989]
x4    345    x4    1345         [x4  345+/-1000=1345 or -665 i.e all the values bw 1345 and -665 with a similar key x4 those are 1345]
x10    8767736477736    x10    8767736478730                   [same like above]
xx    234    xx    1234                                                     [same like above]
xy    387889999    xy    387888999                                   [same like above]



---------- Post updated at 07:38 PM ---------- Previous update was at 06:22 AM ----------

Guys i treid but still a bug in the file

Code:
awk 'NR==FNR{a[$1]=$2;next} a[$1] && ($2 <= a[$1]+1000) && ($2 >= a[$1]-1000) {print a[$1], $0}' test1.txt test2.txt

output for the above code

Code:
100  x1    1010
100  x1    1100
100  x1    900
100  x1    800
989  x3    1989
345  x4    1345
8767736477736  x10    8767736478730
234  xx    1234
387889999	xy    387888999



---------- Post updated at 07:43 PM ---------- Previous update was at 07:38 PM ----------

and it would be grateful if you suggest how to define other columns in the same script like a[$1]=$1 .....so that i can print all the other column along with the output

Last edited by repinementer; 11-17-2009 at 12:41 AM..
# 10  
Old 11-17-2009
Quote:
Originally Posted by repinementer
output
Code:
x1 10 hfffhf 646474_jhg x1 -990 jgjgjggjhgh
x1 10 hfffhf 646474_jhg x1 1010 nbnmmbmb
x2 100 jkfgjj 765755_jg x2 1100 ghjgjhg
x2 100 jkfgjj 765755_jg x2 -900 jghgh

This will match your required output and hopefully solve your problem Smilie
Code:
awk 'NR==FNR{a[$1]=$1;b[$1]=$2;c[$1]=$0;next}a[$1] && b[$1]<($2+x) && b[$1]>($2-x){print c[$1],$0}' x=1001  input1 input2
x1 10 hfffhf 646474_jhg x1 -990 jgjgjggjhgh
x1 10 hfffhf 646474_jhg x1 1010 nbnmmbmb
x2 100 jkfgjj 765755_jg x2 1100 ghjgjhg
x2 100 jkfgjj 765755_jg x2 -900 jghgh

# 11  
Old 11-17-2009
Hi.Thanx for explaining array definition by using a,b and c. Very helpful. The result for your code ouput is as almost same as mine. The bold letters in desired output below I high lighted are missing.? Do you know why?

input1
Code:
x1    10
x1    100
x2    1000
x2    10000
x3    989
x4    345
x10    8767736477736
xx    234
xy    387889999

input2
Code:
x1    1010
x1    1100
x1    900
x1    800
x1    10000
x2    2000
x2    3000
x3    1989
x3    2000
x4    1345
x4    1500
x10    8767736478730
x10    10000000
xx    1234
xy    387888999

Output_for_above_Codes
Code:
x1    100  x1    1010
x1    100  x1    1100
x1    100  x1    900
x1    100  x1    800
x3    989  x3    1989
x4    345  x4    1345
x10    8767736477736  x10    8767736478730
xx    234  xx    1234
xy    387889999 xy    387888999


DesiredOutput
Code:
x1    10    x1    800    
x1    10    x1    900
x1    10    x1    1010    
x1    100    x1    1100
x1    100    x1    800
x1    100    x1    900
x1    100    x1    1010
x2    1000    x2    2000      
x2    10000    NULL    NULL            
x3    989    x3    1989         
x4    345    x4    1345        
x10    8767736477736    x10    8767736478730                   
xx    234    xx    1234                                                     
xy    387889999    xy    387888999

# 12  
Old 11-17-2009
Try this solution:
Code:
# cat awk.script
NR==FNR{
                a[$0]=$0
                next
        }
        {
                b[$0]=$0
                next
        }
END{
        for(x in a){
                        for(y in b){
                                        split(a[x],c)
                                        split(b[y],d)
                                        if( c[1]==d[1] &&
                                            ( d[2] <= c[2] + 1000 ) &&
                                            ( d[2] >= c[2] - 1000 ) \
                                           ){
                                                print a[x] "\t" b[y]
                                            }
                                   }
                   }
    }
# awk -f awk.script input1 input2 | sort
x1    10        x1    1010
x1    10        x1    800
x1    10        x1    900
x1    100       x1    1010
x1    100       x1    1100
x1    100       x1    800
x1    100       x1    900
x10    8767736477736    x10    8767736478730
x2    1000      x2    2000
x3    989       x3    1989
x4    345       x4    1345
xx    234       xx    1234
xy    387889999 xy    387888999

Quote:
Code:
x2    10000    NULL    NULL  

Do you need this line and why ?
# 13  
Old 11-17-2009
Thanx man

NULL is beacuse for the value in input1 key has no referred values with in +/- 1000 range in input2.
It would be great if I have this small one

I'm getting following error.

Code:
$ awk -f awk.script test1.txt test2.txt
gawk: awk.script:16:                                             ( d[2] >= c[2]
- 1000 ) \
gawk: awk.script:16:
         ^ backslash not last character on line

I can run the script with out backslash. whats the significance of \

Last edited by repinementer; 11-17-2009 at 11:44 PM..
# 14  
Old 11-18-2009
Let's try again
Code:
# cat awk.script
BEGIN   {
        t="\t"
        n="NULL"
        }
NR==FNR{
                a[$0]=$0
                next
        }
        {
                b[$0]=$0
                next
        }
END{
        for(x in a){
                        for(y in b){
                                        split(a[x],c)
                                        split(b[y],d)
                                        if( c[1]==d[1]                  &&\
                                            ( d[2] <= c[2] + 1000 )     &&\
                                            ( d[2] >= c[2] - 1000 )       \
                                           ){
                                                print a[x] t b[y]
                                                o[a[x]]++
                                            }
                                   }
                        if(!o[a[x]])       {
                                        print a[x] t n t n
                                   }
                   }
    }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add values to file in 2 new columns

Columns 4 and 5 are X and Y coordinates, column 6 is the elevation I would like to add 2 new columns at the end of the file with values the distance between first(X)(Y) and last location (X)(Y), based in 2 rows the difference in elevation = ($6-prev6) How to calculate the requested values... (6 Replies)
Discussion started by: jiam912
6 Replies

2. Shell Programming and Scripting

Add the values of the lines according to a condition

hi everybody :) I am a beginner in bash and I want to convert the result I have here I want it to be grouped by IP address so iwanna get for each ip adsress the addition of all bandwidth where ip is 100.1.1.15 in other words for 100.1.1.15 it groups me all the values whose ip address is... (11 Replies)
Discussion started by: aynar
11 Replies

3. Programming

How to add one to each row values and keep it after the value in the column?

Dear Folks Hello I have a column of numbers, say: 26 79 68 I want to add one to each row value and get this desire column: 26 27 79 80 68 69 (6 Replies)
Discussion started by: sajmar
6 Replies

4. Shell Programming and Scripting

Add 0 values to replace empty value

Hi Guys. Please can you help me to add 0 values. Starting in column 121 Imput file A 4175.0 8055.01211 75 1 -2172671 77 45 16 457626.4 2609265.1 131.3 1090 102 1 1T N/A124 15 0.8 1051670971100000 A 4175.0 8055.012 7 75 2 -5204072 78 43 25... (10 Replies)
Discussion started by: jiam912
10 Replies

5. Shell Programming and Scripting

Add values in 2 columns and subtract from third

Hi All, I have a file with thousands of lines in the following format, where Field1=First 8 characters Field2-9-16 characters Field3=17-26 characters I need to add Field 1 and Field2 and subtract the result from Field 3. Field3=Field3 - (Field1 + Field2) 0012.00 0010.00 0001576.53... (4 Replies)
Discussion started by: nua7
4 Replies

6. Shell Programming and Scripting

unique entry add values

Hi, I have a file with 3 columns ABC 3 1 ABC 5 1 XYZ 4 2 DEF 3 2 DEF 4 1 DEF 6 1 MNO 5 5 JKL 3 2 JKL 4 2 PQR 12 1 For each unique entry in column 1 I want to add values in column 2 and column3 o/p ABC 8 2 XYZ 4 2 (1 Reply)
Discussion started by: Diya123
1 Replies

7. Shell Programming and Scripting

Add headerline to the values

if header line starts with chr1 and span 10 print chr1 and the original values and ov+10 and viceversa input variableStep chrom=chr1 span=10 191 1 201 18 211 1 variableStep chrom=chr2 span=10 191 11 201 1 211 12 output chr1 191 201 1 chr1 201 211 ... (4 Replies)
Discussion started by: quincyjones
4 Replies

8. Shell Programming and Scripting

Command to add 1000 spaces to end of line

hi, could anyone tell me the command to append spaces at the end of the line. for example, i need 1000 spaces after the word "helloworld" echo "helloworld " i need to achieve this in someother way hardcoding 1000 spaces is not practical. as i am totally new... (3 Replies)
Discussion started by: kavithacs
3 Replies

9. Shell Programming and Scripting

Help: How do I ADD non-integer (decimal) values?

I am trying to create a script that will read from a file two non-integer values (decimals) and add those values together. For example, I want to add 1.51 and -2.37 together and get the sum. Any ideas? Thanks! (2 Replies)
Discussion started by: limshady411
2 Replies

10. Shell Programming and Scripting

How to add two large values

Hi, Gives me wrong value when, $ echo `expr 2221753117 + 299363384` -1773850795 How to overcome this? Appreciate any help on this. -Om (5 Replies)
Discussion started by: Omkumar
5 Replies
Login or Register to Ask a Question