LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-29-2013, 03:07 AM   #1
secret
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Rep: Reputation: Disabled
awk - multiple of a variable


Hi im trying to write a bash script that uses awk
Its supposed to go into folder 0 to 29, there into a specific folder and operate awk on a file.

The problem lies with my awk line.

#!/bin/bash -f

path=some-path
for ((i=0;i<30;i++))
do
curr=$path/$i/parameters
cd $curr

for ((k=0;k<30;k++))
do
for ((l=1;l<31;l++))
do
awk -v k=$k -v l=$l 'BEGIN{count=0}{if((k*6)<=$3 && $3<(l*6))count++}END{print count/10098}' angle-deviation.txt > $k-awk-test

done
done
done

The problem is the k*6 and l*6
In general it is supposed to bin the file. And one bin goes in steps of 6

so to say if the value of $3 is between 0 and 6 count and print the count divided by a number. The bins should go up to 180.
The binning itself works fine if i write all the lines out using explicit numbers.
Thanks for the help.
 
Old 05-29-2013, 04:24 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
It might be me but it isn't clear to me what you are trying to do and what problem you run into.

Could you elaborate and give some (relevant) input and expected output examples?
 
Old 05-29-2013, 04:33 AM   #3
secret
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
for ((k=0;k<30;k++))
do
for ((l=1;l<31;l++))
do
awk -v k=$k -v l=$l 'BEGIN{count=0}{if((k*6)<=$3 && $3<(l*6))count++}END{print count/10098}' angle-deviation.txt > $k-awk-test

done
done

i want it to replace

awk 'BEGIN{count=0}{if(0<=$3 && $3<6)count++}END{print count/10098}'
awk 'BEGIN{count=0}{if(6<=$3 && $3<12)count++}END{print count/10098}'
awk 'BEGIN{count=0}{if(12<=$3 && $3<18)count++}END{print count/10098}'
awk 'BEGIN{count=0}{if(18<=$3 && $3<24)count++}END{print count/10098}'

and so on. Output is
count for range 0 to 6 divided by 10098
count for range 6 to 12 divided by 10098
count for range 12 to 18 divided by 10098
count for range 18 to 24 divided by 10098

and theres no error or anything but the numbers it gives me are wrong compared to the explicitely written script without the loop. I tried eliminating the error and i guess its the multiplication of the variable.
Since i loop in steps of 1 and in awk i multiply the variable (k*6) so its supposed to be value of k multiplied by 6 which should give me steps of 6 instead of 1. since 0*6= 0, 1*6=6 2*6=12 and so on.
 
Old 05-29-2013, 04:46 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
please use [code]here comes the code[/code] to keep formatting.
maybe this:
Code:
for ((k=0;k<30;k++))
do
  for ((l=1;l<31;l++))
  do
    kk=$(( k * 6 ))
    ll=$(( l * 6 ))
    awk 'BEGIN{count=0}'$kk'<=$3 && $3<'$ll'{count++}END{print count/10098}'
  done
done
not tested, but it is still not really efficient.
 
Old 05-29-2013, 05:52 AM   #5
secret
LQ Newbie
 
Registered: Mar 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
thx for the answers. That works pan.
 
Old 05-29-2013, 07:23 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
glad to help you
if you really want to say thanks just press yes
 
1 members found this post helpful.
Old 05-29-2013, 10:55 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
My question would be what is the point of the 2 inner loops when you could have your single bash loop and a single awk doing all the work?

Something like:
Code:
for path in some-path/{0..29}/parameters
do
    awk '{for(k=0;k<30;k++){count=0;for(l=1;l<31;l++)if(k*6 <= $3 && $3 < l*6)count++;print count / 10098 > gensub(/[^/]*$/,"","1",FILENAME)k"-awk-test"}}' "$path/angle-deviation.txt"
done
Now I haven't checked it exactly, but I am sure you get the drift
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Replacing multiple string in multiple files with awk jnorbert Linux - Newbie 9 03-26-2013 12:39 PM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration