0

I have a Buch of files (3300) which are the result of my genome-scan output anacovis2_1_summary_betai_reg.out ... anacovis2_3300_summary_betai_reg.out and each file look like this (fist few lines):

 1    4996     0.03907811     0.19369659   -10.43580084     0.00150707     0.00836902     0.06697258
  1    4997     0.06213154     0.17373333   -10.98540609     0.00213014     0.00556877     0.15361369
  1    4998    -0.00284978     0.19418451    -8.81547738     0.00016505     0.00741737     0.00777931
  1    4999    -0.02047544     0.19574268    -9.12692867    -0.00059062     0.00632552     0.03357265
  1    5000    -0.01769435     0.18560835   -13.15854481    -0.00038595     0.00540918     0.02543350
  2       1     0.04259550     0.20256840   -10.98339784     0.00120126     0.00529516     0.08590396
  2       2    -0.10782050     0.17555969    -9.13783036    -0.00355861     0.00689091     0.21784244
  2       3     0.02548854     0.18571440   -15.42307129     0.00006131     0.00291038     0.00736142
  2       4     0.03084782     0.17813247   -11.99911720     0.00109688     0.00630034     0.06459986

the first column is environmental variables ranging from 1 to 26. I want to go through each file and extract only the 4th column for each environmental variable and save them in a file with a suffix of environmental variable number.

I know how to do it separately for each enviornmental variable for example for the variable 1

awk '($1==1){print $4>FILENAME"_env1"}'anacovis2_{1..3300}_summary_betai_reg.out

or for the variable 2

awk '($1==2){print $4>FILENAME"_env2"}'anacovis2_{1..3300}_summary_betai_reg.out

but it takes time if I want to do it like this, I wonder whether I can do it faster in a loop for instance. I tried something like this

for i in {1..26};
do awk '($1==i){print $4>FILENAME"_i"}'anacovis2_{1..3300}_summary_betai_reg.out
done

but did not work! Can someone help me to sort this out? thanks

  • why not just awk '{ print $4 >> FILE_$1 }' ? – DopeGhoti Oct 19 '17 at 22:01
  • @Shlublu, no it did not work after applying your suggestion – Anna1364 Oct 19 '17 at 22:34
  • @Anna1364 Sorry Anna. Removed! – Shlublu Oct 20 '17 at 9:41
1

Yes, there is. Do it as this way with awk itself only.

awk '{print $4>FILENAME"_env"$1}' anacovis2_{1..3300}_summary_betai_reg.out

To save each same env from all files into same one file, like env1, env2 and so on just drop the FILENAME there and run the command with {print $4>"env"$1}.

| improve this answer | |
  • yes, it did the job, I wonder what is wrong in my loop. I modified it: for i in {1..3300}; do awk '($1==$i){print $4>FILENAME"_$i"}'anacovis2_{1..3300}_summary_betai_reg.out done but did not work – Anna1364 Oct 19 '17 at 22:35
  • The problem is that the shell variable i is not available within strong quotes ('), so it was an unitialized awk variable, and getting parsed by awk as $0. – DopeGhoti Oct 19 '17 at 22:36
  • @afshin,sorry one thing that I could not understand in your code; _env"$1 what does that mean? I was expecting it will only print out variable1? – Anna1364 Oct 19 '17 at 23:17
  • Hi the FILENAME is you know it's Orginal filename for each file awk reads from. and _env is a suffix and $1 will the value of first column. so like FILENAME_env1, FILENAME_env2, ..., FILENAME_env26 – αғsнιη Oct 20 '17 at 1:19
  • Aha, thanks so much.Is there also anyway to save the outputs for each env in a separete file file while I am running this code: awk '{print $4>FILENAME"_env"$1}' anacovis2_{1..3300}_summary_betai_reg.out? – Anna1364 Oct 20 '17 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.