AWK/Bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK/Bash script
# 1  
Old 05-10-2012
AWK/Bash script

I would like to write a script to extend this command to a general case:


Code:
BEGIN {s_0=0;n_0=0}{n_0++;s_0+=($51-$1)^2}END {print sqrt(s_0/n_0)}

i.e. so that

Code:
BEGIN {s_0=0;n_0=0}{n_0++;s_0+=($51-$1)^2}END {print sqrt(s_0/n_0)}
BEGIN {s_1=0;n_1=0}{n_1++;s_1+=($51-$2)^2}END {print sqrt(s_1/n_1)}
BEGIN {s_2=0;n_2=0}{n_2++;s_2+=($51-$3)^2}END {print sqrt(s_2/n_2)}
...
BEGIN {s_50=0;n_50=0}{n_50++;s_50+=($51-$51)^2}END {print sqrt(s_50/n_50)}


I already have a script that could loop over the variable s and n,
any ideas?

Last edited by Scrutinizer; 05-10-2012 at 04:46 PM.. Reason: Changed quote tags to code tags
# 2  
Old 05-10-2012
It'd be better if you can tell us your requirement, provide sample input and an example of desired output.
# 3  
Old 05-10-2012
a loop over field 1 to 51:

Code:
{ for (i=1;i<=51;i++) { n[i]++; s[i]+=($51-$i)^2; }
END { for (i=1;i<=51;i++) { print sqrt(s[i]/n[i]) }

i'd like to say n[] isn't unique and does not need to be an array. it also could probably be replaced with NR. print sqrt(s[i]/NR)
# 4  
Old 05-10-2012
What, exactly, is this single case supposed to do?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash/awk script problem

Hi, I have 100 files containing different values in single column, I want to split those files in two separate files (file2 and file3) based on average value of first column of each file, for those files I am working on the following script #bin/bash for memb in $(seq 1 100) do awk... (4 Replies)
Discussion started by: dsp80
4 Replies

2. Shell Programming and Scripting

Bash script with awk command ERROR

Hello im new here... Im trying to read file and create folders from words in it but i get this for loop error awk : line 3 : syntax error at or near for my code is.. #!/bin/bash begin for (( i=1;i<=5;i++)); do awk -v i=$i $0 { print $i } mkdir $i done {print $i} end {} i have... (7 Replies)
Discussion started by: boxstep
7 Replies

3. Shell Programming and Scripting

Converting awk script from bash to csh

I have the following script set up and working properly in bash. It basically copies a set of lines which match "AS1100002" from one file and replaces the same lines in another file. awk -vN=AS1100002* 'NR==FNR { if($1 ~ N)K=$0; next } { if($1 in K) $0=K; print }' $datadir/file1... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

4. Shell Programming and Scripting

How to Pass filename to AWK in bash script

I have written a script which works fine, to remove patterns contained in EXCLUDE.DAT from input.txt awk 'BEGIN {n=0;while (getline < "EXCLUDE.DAT" > 0){ex=$0;n++}} {for(var in ex){print var "-" ex $0 ;i++}}' input.txt The last problem I need to solve is how to pass the file... (3 Replies)
Discussion started by: nixie
3 Replies

5. Shell Programming and Scripting

Help: How to convert this bash+awk script in awk script only?

This is the final first release of the dynamic menu generator for pekwm (WM). #!/bin/bash function param_val { awk "/^${1}=/{gsub(/^${1}="'/,""); print; exit}' $2 } echo "Dynamic {" for CF in `ls -c1 /usr/share/applications/*.desktop` do name=$(param_val Name $CF) ... (3 Replies)
Discussion started by: alexscript
3 Replies

6. Shell Programming and Scripting

XML- Sed || Awk Bash script... Help!

Hi ! I'm working into my first bash script to make some xml modification and it's going to make me crazy lol .. so I decide to try into this forum to take some ideas from people that really know about this! This is my situation I've and xml file with a lots of positional values with another tags... (9 Replies)
Discussion started by: juampal
9 Replies

7. Shell Programming and Scripting

Using AWK in a bash script

So I am a newbie obviously but I need some help. I am trying to run a script to check that the number of fields in a database is equal to 4, if not they should try again and run an appropriate database file. The fields are separated by a semi colon. I've tried a lot of things, this what I'm... (3 Replies)
Discussion started by: mb001
3 Replies

8. Shell Programming and Scripting

AWK manipulation in bash script

EDIT: This has been SOLVED. Thanks! Greetings everyone, I've posted a few threads with some quick help questions, and this is another one of those. I can't post enough gratitude for those much more knowledgeable than myself who are willing to give good advice for my minor issues. Now,... (2 Replies)
Discussion started by: Eblue562
2 Replies

9. UNIX for Dummies Questions & Answers

using awk inside bash script?

Hello, I'm trying to write a bash script that will query the current system time (OS X 10.6.6) and then convert the output from HH:MM:SS into time in seconds. The output of the system time command (systemsetup -gettime) is returned as: Time: HH:MM:SS so I wanted to use awk -F: to grab... (5 Replies)
Discussion started by: xaiu
5 Replies

10. Shell Programming and Scripting

Help with Bash script - set, awk

Trying to search a log file for a string, starting from a certain point in the log file. I want to return the number of lines that contain the search string and the total number of lines in the log file. Here's the part of the script I'm having problems with: set -- $(awk -v... (12 Replies)
Discussion started by: mglenney
12 Replies
Login or Register to Ask a Question