Sponsored Content
Top Forums Shell Programming and Scripting Calculating average for every Nth line in the Nth column Post 302622493 by ncwxpanther on Thursday 12th of April 2012 08:17:14 AM
Old 04-12-2012
Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation?

I have a data file that is in the format of
Code:
1944-12,5.6
1945-01,9.8
1945-02,6.7
1945-03,9.3
1945-04,5.9
1945-05,0.7
1945-06,0.0
1945-07,0.0
1945-08,0.0
1945-09,0.0
1945-10,0.2
1945-11,10.5
1945-12,22.3
1946-01,35.2
1946-02,13.4

I need to find the average of the values contained within -01, -02, and -03.

For instance the average of
Code:
1945-01,9.8
1945-02,6.7
1945-03,9.3

Would output
Code:
1945-03, 8.6

Any help would be appreciative.
Thanks!

Last edited by Franklin52; 04-12-2012 at 09:18 AM.. Reason: Please use code tags for code and data samples, thank you
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Editing 1st or nth column

Hi, I have a file whick is pipe delimited : 100| alpha| tabgo|watch| |||| 444444 | alpha| tabgo|watch| |||| 444444 | sweden |tabgo|watch| |||| 444444 | US| tabgo|watch| |||| 444444 100| factory| tabgo|watch| |||| 444444 | ABC| tabgo|watch| |||| 444444 | launch| tabgo|watch| ||||... (4 Replies)
Discussion started by: darshanw
4 Replies

2. Shell Programming and Scripting

get 3rd column of nth line

hi; i have a file.txt and its 9th, 10th and 11th line lines are: RbsLocalCell=S2C1 maxPortIP 4 (this is 9th line) RbsLocalCell=S3C1 maxPortIP 4 (this is 10th line) RbsLocalCell=S1C1 ... (11 Replies)
Discussion started by: gc_sw
11 Replies

3. Shell Programming and Scripting

Finding Nth Column

Please help me how can I display every nth field present in a "|" delimited file. Ex: If a have a file with data as a|b|c|d|e|f|g|h|k|l|m|n I want to display every 3rd feild which means the output should be c f k n Please help me. (1 Reply)
Discussion started by: ngkumar
1 Replies

4. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

5. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

6. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

7. Shell Programming and Scripting

Replace nth to nth character?

Hi I got the following problem and I wonder if some could please help me out? I'd like to replace character 8 - 16 , 16 - 24 cat file ... (2 Replies)
Discussion started by: stinkefisch
2 Replies

8. Shell Programming and Scripting

Taking nth column and putting its value in n+1 column using awk

Hello Members, Need your expert opinion how to tackle below. I have an input file that looks like below: USS|AWCC|AFGAW|93|70 USSAA|Roshan TDCA|AFGTD|93|72,79 ALB|Vodafone|ALBVF|355|69 ALGEE|Wataniya (Nedjma)|DZAWT|213|50,550 I like output file in below format: ... (7 Replies)
Discussion started by: umarsatti
7 Replies

9. UNIX for Dummies Questions & Answers

Getting the lines with nth column non-null

Hi, I have a huge list of archives (.gz). Each archive is about 40MB. A file is generated every minute so if I want to analyze the data for 1 hour I get already 60 files for example. These are text files, ';' separated, each line having about 300 fields (columns). What I need to do is to... (11 Replies)
Discussion started by: Nenad
11 Replies

10. Shell Programming and Scripting

Replace Value of nth Column of Each Line Using Array

Hello All, I am writing a shell script with following requirement: 1. I have one input file as below CHE01,A,MSC,INO CHE02,B,NST,INC CHE03,C,STM,INP 2. In shell script I have predefined array as below: Array1={A, B, C} Array2= {U09, C04, A054} (6 Replies)
Discussion started by: angshuman
6 Replies
apache_mod_perl-108~358::mod_perl-2.0.7::docs::api::ApacUser:Contributed Perapache_mod_perl-108~358::mod_perl-2.0.7::docs::api::Apache2::Access(3)

NAME
Apache2::Access - A Perl API for Apache request object: Access, Authentication and Authorization. Synopsis use Apache2::Access (); # allow only GET method $r->allow_methods(1, qw(GET)); # Apache Options value $options = $r->allow_options(); # Apache AllowOverride value $allow_override = $r->allow_overrides(); # which Options are allowed by AllowOverride (since Apache 2.2) $allow_override_opts = $r->allow_override_opts(); # auth name ("foo bar") $auth_name = $r->auth_name(); # auth type $auth_type = $r->auth_type(); $r->auth_type("Digest"); # Basic authentication process my ($rc, $passwd) = $r->get_basic_auth_pw(); # the login name of the remote user (RFC1413) $remote_logname = $r->get_remote_logname(); # dynamically figure out which auth has failed $r->note_auth_failure(); # note Basic auth failure $r->note_basic_auth_failure(); # note Digest auth failure $r->note_digest_auth_failure(); # Apache Request value(s) $requires = $r->requires(); # Apache Satisfy value (as a number) $satisfy = $r->satisfies(); # check whether some auth is configured $need_auth = $r->some_auth_required(); Description The API provided by this module deals with access, authentication and authorization phases. "Apache2::Access" extends "Apache2::RequestRec". API
"Apache2::Access" provides the following functions and/or methods: "allow_methods" Specify which HTTP methods are allowed $r->allow_methods($reset); $r->allow_methods($reset, @methods); obj: $r ( "Apache2::RequestRec object" ) The current request arg1: $reset ( boolean ) If a true value is passed all the previously allowed methods are removed. Otherwise the list is left intact. opt arg2: @methods ( array of strings ) a list of HTTP methods to be allowed (e.g. "GET" and "POST") ret: no return value since: 2.0.00 For example: here is how to allow only "GET" and "POST" methods, regardless to what was the previous setting: $r->allow_methods(1, qw(GET POST)); "allow_options" Retrieve the value of "Options" for this request $options = $r->allow_options(); obj: $r ( "Apache2::RequestRec object" ) The current request ret: $options ( integer ) the "Options" bitmask. Normally used with bitlogic operators against "Apache2::Const :options constants". since: 2.0.00 For example if the configuration for the current request was: Options None Options Indexes FollowSymLinks The following applies: use Apache2::Const -compile => qw(:options); $r->allow_options & Apache2::Const::OPT_INDEXES; # TRUE $r->allow_options & Apache2::Const::OPT_SYM_LINKS; # TRUE $r->allow_options & Apache2::Const::OPT_EXECCGI; # FALSE "allow_overrides" Retrieve the value of "AllowOverride" for this request $allow_override = $r->allow_overrides(); obj: $r ( "Apache2::RequestRec object" ) The current request ret: $allow_override ( integer ) the "AllowOverride" bitmask. Normally used with bitlogic operators against "Apache2::Const :override constants". since: 2.0.00 For example if the configuration for the current request was: AllowOverride AuthConfig The following applies: use Apache2::Const -compile => qw(:override); $r->allow_overrides & Apache2::Const::OR_AUTHCFG; # TRUE $r->allow_overrides & Apache2::Const::OR_LIMIT; # FALSE "allow_override_opts" Retrieve the bitmask of allowed "Options" set by "AllowOverride Options=..." for this request $override_opts = $r->allow_override_opts(); Enabling single options was introduced in Apache 2.2. For Apache 2.0 this function returns "Apache2::Const::OPT_UNSET" | "Apache2::Const::OPT_ALL" | "Apache2::Const::OPT_INCNOEXEC" | "Apache2::Const::OPT_SYM_OWNER" | "Apache2::Const::OPT_MULTI", which corresponds to the default value (if not set) for Apache 2.2. obj: $r ( "Apache2::RequestRec object" ) The current request ret: $override_opts ( integer ) the override options bitmask. Normally used with bitlogic operators against "Apache2::Const :options constants". since: 2.0.3 For example if the configuration for the current request was: AllowOverride Options=Indexes,ExecCGI The following applies: use Apache2::Const -compile => qw(:options); $r->allow_override_opts & Apache2::Const::OPT_EXECCGI; # TRUE $r->allow_override_opts & Apache2::Const::OPT_SYM_LINKS; # FALSE "auth_name" Get/set the current Authorization realm (the per directory configuration directive "AuthName"): $auth_name = $r->auth_name(); $auth_name = $r->auth_name($new_auth_name); obj: $r ( "Apache2::RequestRec object" ) The current request opt arg1: $new_auth_name ( string ) If $new_auth_name is passed a new "AuthName" value is set ret: "$" ( integer ) The current value of "AuthName" since: 2.0.00 The "AuthName" directive creates protection realm within the server document space. To quote RFC 1945 "These realms allow the protected resources on a server to be partitioned into a set of protection spaces, each with its own authentication scheme and/or authorization database." The client uses the root URL of the server to determine which authentication credentials to send with each HTTP request. These credentials are tagged with the name of the authentication realm that created them. Then during the authentication stage the server uses the current authentication realm, from "$r->auth_name", to determine which set of credentials to authenticate. "auth_type" Get/set the type of authorization required for this request (the per directory configuration directive "AuthType"): $auth_type = $r->auth_type(); $auth_type = $r->auth_type($new_auth_type); obj: $r ( "Apache2::RequestRec object" ) The current request opt arg1: $new_auth_type ( string ) If $new_auth_type is passed a new "AuthType" value is set ret: "$" ( integer ) The current value of "AuthType" since: 2.0.00 Normally "AuthType" would be set to "Basic" to use the basic authentication scheme defined in RFC 1945, Hypertext Transfer Protocol -- HTTP/1.0. However, you could set to something else and implement your own authentication scheme. "get_basic_auth_pw" Get the password from the request headers my ($rc, $passwd) = $r->get_basic_auth_pw(); obj: $r ( "Apache2::RequestRec object" ) The current request ret1: $rc ( "Apache2::Const constant" ) "Apache2::Const::OK" if the $passwd value is set (and assured a correct value in "$r->user"); otherwise it returns an error code, either "Apache2::Const::HTTP_INTERNAL_SERVER_ERROR" if things are really confused, "Apache2::Const::HTTP_UNAUTHORIZED" if no authentication at all seemed to be in use, or "Apache2::Const::DECLINED" if there was authentication, but it wasn't "Basic" (in which case, the caller should presumably decline as well). ret2: $ret (string) The password as set in the headers (decoded) since: 2.0.00 If "AuthType" is not set, this handler first sets it to "Basic". "get_remote_logname" Retrieve the login name of the remote user (RFC1413) $remote_logname = $r->get_remote_logname(); obj: $r ( "Apache2::RequestRec object" ) The current request ret: $remote_logname ( string ) The username of the user logged in to the client machine, or an empty string if it could not be determined via RFC1413, which involves querying the client's identd or auth daemon. since: 2.0.00 Do not confuse this method with "$r->user", which provides the username provided by the user during the server authentication. "note_auth_failure" Setup the output headers so that the client knows how to authenticate itself the next time, if an authentication request failed. This function works for both basic and digest authentication $r->note_auth_failure(); obj: $r ( "Apache2::RequestRec object" ) The current request ret: no return value since: 2.0.00 This method requires "AuthType" to be set to "Basic" or "Digest". Depending on the setting it'll call either "$r->note_basic_auth_failure" or "$r->note_digest_auth_failure". "note_basic_auth_failure" Setup the output headers so that the client knows how to authenticate itself the next time, if an authentication request failed. This function works only for basic authentication $r->note_basic_auth_failure(); obj: $r ( "Apache2::RequestRec object" ) The current request ret: no return value since: 2.0.00 "note_digest_auth_failure" Setup the output headers so that the client knows how to authenticate itself the next time, if an authentication request failed. This function works only for digest authentication. $r->note_digest_auth_failure(); obj: $r ( "Apache2::RequestRec object" ) The current request ret: no return value since: 2.0.00 "requires" Retrieve information about all of the requires directives for this request $requires = $r->requires obj: $r ( "Apache2::RequestRec object" ) The current request ret: $requires ( ARRAY ref ) Returns an array reference of hash references, containing information related to the "require" directive. since: 2.0.00 This is normally used for access control. For example if the configuration had the following require directives: Require user goo bar Require group bar tar this method will return the following datastructure: [ { 'method_mask' => -1, 'requirement' => 'user goo bar' }, { 'method_mask' => -1, 'requirement' => 'group bar tar' } ]; The requirement field is what was passed to the "Require" directive. The method_mask field is a bitmask which can be modified by the "Limit" directive, but normally it can be safely ignored as it's mostly used internally. For example if the configuration was: Require user goo bar Require group bar tar <Limit POST> Require valid-user </Limit> and the request method was "POST", "$r->requires" will return: [ { 'method_mask' => -1, 'requirement' => 'user goo bar' }, { 'method_mask' => -1, 'requirement' => 'group bar tar' } { 'method_mask' => 4, 'requirement' => 'valid-user' } ]; But if the request method was "GET", it will return only: [ { 'method_mask' => -1, 'requirement' => 'user goo bar' }, { 'method_mask' => -1, 'requirement' => 'group bar tar' } ]; As you can see Apache gives you the requirements relevant for the current request, so the method_mask is irrelevant. It is also a good time to remind that in the general case, access control directives should not be placed within a <Limit> section. Refer to the Apache documentation for more information. Using the same configuration and assuming that the request was of type POST, the following code inside an Auth handler: my %require = map { my ($k, $v) = split /s+/, $_->{requirement}, 2; ($k, $v||'') } @{ $r->requires }; will populate %require with the following pairs: 'group' => 'bar tar', 'user' => 'goo bar', 'valid-user' => '', "satisfies" How the requires lines must be met. What's the applicable value of the "Satisfy" directive: $satisfy = $r->satisfies(); obj: $r ( "Apache2::RequestRec object" ) The current request ret: $satisfy ( integer ) How the requirements must be met. One of the "Apache2::Const :satisfy constants": "Apache2::Const::SATISFY_ANY", "Apache2::Const::SATISFY_ALL" and "Apache2::Const::SATISFY_NOSPEC". since: 2.0.00 See the documentation for the "Satisfy" directive in the Apache documentation. "some_auth_required" Can be used within any handler to determine if any authentication is required for the current request: $need_auth = $r->some_auth_required(); obj: $r ( "Apache2::RequestRec object" ) The current request ret: $need_auth ( boolean ) TRUE if authentication is required, FALSE otherwise since: 2.0.00 See Also mod_perl 2.0 documentation. Copyright mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0. Authors The mod_perl development team and numerous contributors. perl v5.16.2 2011-02-apache_mod_perl-108~358::mod_perl-2.0.7::docs::api::Apache2::Access(3)
All times are GMT -4. The time now is 06:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy