Loop over awk or passing parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop over awk or passing parameter
# 1  
Old 12-02-2014
Loop over awk or passing parameter

I wrote this script which works well when I manually input 55518622 and 1
but I need this script to be generic and loop over the following table
Code:
awk '$4>(55518622-500000) && $4<(55518622+500000)' chr1_GEN2bim | awk 'BEGIN {min=1000000000; max=0;}; {\
 if($4<min && $4 != "") min = $4; if($4>max && $4 != "") max = $4; } END {print "chr"$1"\t"min"\t"max}'


Given
Code:
1       55518622        rs45613943      1.983654e-08
1       109815252       rs611917        3.439871e-11
2       98323821        kgp8891906      3.619327e-19
10      15912165        kgp3074751      4.737534e-15
15      58723426        rs1077835       8.918459e-11
16      56686970        rs148916841     2.375065e-12
19      11187358        rs144826254     3.641029e-11
19      45373276        rs11879589      1.034183e-09


I want a one off output like
Code:
chr1 minValue  maxValue
chr2 minValue maxValue
chr10 minValue maxValue
chr15 minvalue maxValue
chr16 minValue maxValue
chr19 minValue maxValue

my chr1_GEN2bim looks like this
Code:
1       rs7514195       0       198000135       
1       rs6667378       0       198000253       
1       rs114753897     0       198000439

# 2  
Old 12-02-2014
This is quite incomplete a specification. Your "Given" is in a file? You want to parse multiple input files named "chrn_GEN2bim" where n comes from your "Given" file? Is the first column of those input files always identical to n?
# 3  
Old 12-02-2014
Hi RudiC,

Thanks for your reply. Sorry I wasnt very clear.
Yes the "Given" is in a file.I want to parse multiple input files named "chr{n}_GEN2bim" where n comes from the first column of my "Given" file.

Thanks

SA
# 4  
Old 12-02-2014
This could probably be optimized a lot if I knew what you were doing. Whenever you do awk | awk | kitchen | sink, you should replace it with one awk and be done with it.

Code:
while read A B C D E
do
        awk '$4>(B-500000) && $4<(B+500000)' B="$B" chr${A}_GEN2bim |
        awk 'BEGIN {min=1000000000; max=0;}; $4 == "" { next }; $4<min { min = $4; }; $4>max { max=$4; } END {print "chr"$1"\t"min"\t"max}'
done < given > output

# 5  
Old 12-02-2014
Do you want a min/max value for each chrn? Then you need to reset them for every new n.
And, you run your awk script twice for e.g. 1 od 19. So there might not be one single min/max for those as the, say, "tolerances" are different for the two calls.
# 6  
Old 12-02-2014
I think this would do it with a single awk, run a handful of times, instead of two awks per line:

Code:
while read A B C D E
do
        echo "B=$B"
        echo "chr${A}_GEN2bim"
done < given | xargs -n 16 awk -v OFS="\t" 'F != FILENAME {
        if(F) print "chr" A, min, max;
        min=1000000000; max=0; F=FILENAME;
}

END { if(F) print "CHR" A, min, max; }

$1 != "" { A=$1; }

($4 != "") && ($4>(B-500000)) && ($4<(B+500000)) {
        if($4 < min) min=$4;
        if($4 > max) max=$4;
}' > output

# 7  
Old 12-02-2014
Sorry, I didnt mention that there are two input files in which I dont know where to fit the second file.
The first file is chr1_GEN2bim. It has this format. Note that everything at first column is 1 because it is chr1_GEN2bim
Code:
1       rs7514195       0       198000135       
1       rs6667378       0       198000253       
1       rs114753897     0       198000439

so if it is chr2_GEN2bim , it would be like the following, also note that everything at first column is 2.
Code:
2       rs7514198       0       378000135       
2       rs66673789       0       98000253       
2       rs11475389     0       18000439

etc. All the files chr1_GEN2bim, chr2_GEN2bim, chr3_GEN2bim, ...chr22_GEN2bim are on the working directory


For the second file (following table), it is where I want to extract from column 1 and 2.
Code:
1       55518622        rs45613943      1.983654e-08
1       109815252       rs611917        3.439871e-11
2       98323821        kgp8891906      3.619327e-19
10      15912165        kgp3074751      4.737534e-15
15      58723426        rs1077835       8.918459e-11
16      56686970        rs148916841     2.375065e-12
19      11187358        rs144826254     3.641029e-11
19      45373276        rs11879589      1.034183e-09

What I actually want to do is read A and B simultaneously from file 2.
A is the value at the first column, while B is at the second column both from second file

I think it should be like this
Code:
while read A B  (in this case A is 1 and B is 55518622, so next A is 2 while B is 109815252, and next A is 10 and B=15912165  )
do
        awk '$4>(B-500000) && $4<(B+500000)' B="$B" chr${A}_GEN2bim |
        awk 'BEGIN {min=1000000000; max=0;}; $4 == "" { next }; $4<min { min = $4; }; $4>max { max=$4; } END {print "chr"$1"\t"min"\t"max}'
done < second_file > output

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

2. UNIX for Dummies Questions & Answers

Passing shell script parameter value to awk command in side the script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies

3. Shell Programming and Scripting

Passing parameter to awk command

Hi, I have a situation where I need to create a SQL statement using Unix script for each value in first field (ID). The file looks like this. Id,TARGET_FIELD_NAME,SOURCE_FIELD_NAME 1,Test_Rate,Field1 1,Test_Factor,Field2 1,Test_Size,Field3 2,Test_Rate,Field4 2,Test_Factor,Field5... (3 Replies)
Discussion started by: kiranredz
3 Replies

4. Shell Programming and Scripting

Passing parameter to script, and split the parameter

i am passing input parameter 'one_two' to the script , the script output should display the result as below one_1two one_2two one_3two if then echo " Usage : <$0> <DATABASE> " exit 0 else for DB in 1 2 3 do DBname=`$DATABASE | awk -F "_" '{print $1_${DB}_$2}` done fi (5 Replies)
Discussion started by: only4satish
5 Replies

5. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

6. Shell Programming and Scripting

Error passing parameter in "sub" command in awk

I have to replace the pattern found in one file in another file with null/empty "" if found on the fields 3 or 4 ONLY File 1 ==== 10604747|Mxdef|9999|9999|9999|2012-03-04 00:00:59 10604747|Mcdef|8888|9999|8888|2012-03-04 00:00:59 . . . File 2 ==== 9999 8888 . . . Expected... (7 Replies)
Discussion started by: machomaddy
7 Replies

7. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

8. Shell Programming and Scripting

Passing parameter in sed or awk commands to print for the specific line in a file

Hi, I am trying to print a specific line in a file through sed or awk. The line number will be passed as a parameter from the previous step. My code looks as below. TEMP3=`sed -n '$TEMP2p' $FILEPATH/Log.txt` $TEMP2, I am getting from the previous step which is a numerical value(eg:3). ... (2 Replies)
Discussion started by: satyasrin82
2 Replies

9. Shell Programming and Scripting

Passing a parameter to AWK

Hi All, I am trying to pass a parameter to AWK on my KSH shell prompt as below. var1=2 echo $var1 awk -v var2=${var1} '{print var2}' testfile.txt I am passing the input file (testfile) to awk to get some o/p. It is having 10 records. When I run AWK, it is throwing the following errors... (1 Reply)
Discussion started by: Raamc
1 Replies

10. Shell Programming and Scripting

Passing a variable to awk while in a shell for loop

I am a newbie to awk and c programming, however am not a unix newbie. However, I do need help with a kshell script I am writing. It is almost complete, the last step is killing me. Any help would be greatly appreciated. What I am trying to do is cat a text file that has usernames. Then, using... (2 Replies)
Discussion started by: synergy_texas
2 Replies
Login or Register to Ask a Question