Sponsored Content
Top Forums Shell Programming and Scripting awk to assign points to variables based on conditions and update specific field Post 303003893 by ctsgnb on Friday 22nd of September 2017 08:31:16 AM
Old 09-22-2017
Did you test your script so far ? have you performed some "unity testing" to validate one by one that your conditions work as you expect ?

In condition 1 , where does this (see in if(index$f["FuncrefGene"]... ) "index$f" come from ?

Why using the dollar sign in variable settings ? when assigning the value 3 to the variable "var2" in awk, shouldn't you use var2=3 rather than $var2=3 ??? same in testing : you are in awk, not in shell so use : if ( var2 == 3 ) { ... } rather than if ( $var2 == 3 ) { ... }

Last edited by ctsgnb; 09-22-2017 at 09:41 AM..
This User Gave Thanks to ctsgnb For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract file records based on some field conditions

Hello Friends, I have a file(InputFile.csv) with the following columns(the columns are pipe-delimited): ColA|ColB|ColC|ColD|ColE|ColF Now for this file, I have to get those records which fulfil the following condition: If "ColB" is NOT NULL and "ColD" has values one of the following... (9 Replies)
Discussion started by: mehimadri
9 Replies

2. Shell Programming and Scripting

awk assign output of array to specific field-number

With this script i want to print the output to a specific field-number . Can anybody help? awk 'NR=FNR{split(FILENAME,fn,"_");nr=$2;f = $1} END{for (i=1;i<=f;i++) print i,$fn=nr}' input_5.csv input_6.csvinput_5.csv 4 135 5 185 6 85 11 30input_6.csv 1 90 3 58 4 135 7 60 8 55 10... (1 Reply)
Discussion started by: sdf
1 Replies

3. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

4. Shell Programming and Scripting

Calculate the total 4 field based on the conditions

Please help me to write a script Match with ACNO & NAME if it matched calculate the total val1 val2 val3 and val4 and GT is total of ACNO wise.please check the output Table ----------------- 1005|ANDP|ACN|20|50|10|30 1005|ANDP|ACN|20|10|30|40 1001|AND|NAC|40|50|40|50... (22 Replies)
Discussion started by: kalia4u
22 Replies

5. Shell Programming and Scripting

awk to update field file based on match

If $1 in file1 matches $2 in file2. Then the value in $2 of file2 is updated to $1"."$2 of file2. The awk seems to only match the two files but not update. Thank you :). awk awk 'NR==FNR{A ; next} $1 in A { $2 = a }1' file1 file2 file1 name version NM_000593 5 NM_001257406... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. Shell Programming and Scripting

awk to update field in file based of match in another

I am trying to use awk to match two files that are tab-delimited. When a match is found between file1 $1 and file2 $4, $4 in file2 is updated using the $2 value in file1. If no match is found then the next line is processed. Thank you :). file1 uc001bwr.3 ADC uc001bws.3 ADC... (4 Replies)
Discussion started by: cmccabe
4 Replies

7. Shell Programming and Scripting

awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in $2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in $2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I... (2 Replies)
Discussion started by: cmccabe
2 Replies

8. Shell Programming and Scripting

awk to update file based on 5 conditions

I am trying to use awk to update the below tab-delimited file based on 5 different rules/conditions. The final output is also tab-delimited and each line in the file will meet one of the conditions. My attemp is below as well though I am not very confident in it. Thank you :). Condition 1: The... (10 Replies)
Discussion started by: cmccabe
10 Replies

9. Shell Programming and Scripting

Perl to update field based on a specific set of rules

In the perl below, which does execute, I am having trouble with the else in Rule 3. The digit in f{8} is extracted and used to update f accordinly along with the value in f. There can be either - * or + before the number that is extracted but the same logic applies, that is if the value is greater... (5 Replies)
Discussion started by: cmccabe
5 Replies

10. Shell Programming and Scripting

Update a specific field in file with Variable value based on other Key Word

I have an input file with A=xyz B=pqr I would want the value in Second Field (xyz or pqr) updated with a value present in Shell Variable based on the value passed in the first field. (A or B ) while read line do NEW_VALUE = `some functionality done on $line` If $line=First Field-... (1 Reply)
Discussion started by: infernalhell
1 Replies
Test::LectroTest::Property(3pm) 			User Contributed Perl Documentation			   Test::LectroTest::Property(3pm)

NAME
Test::LectroTest::Property - Properties that make testable claims about your software SYNOPSIS
use MyModule; # provides my_function_to_test use Test::LectroTest::Generator qw( :common ); use Test::LectroTest::Property qw( Test ); use Test::LectroTest::TestRunner; my $prop_non_neg = Property { ##[ x <- Int, y <- Int ]## $tcon->label("negative") if $x < 0; $tcon->label("odd") if $x % 2; $tcon->retry if $y == 0; # 0 can't be used in test my_function_to_test( $x, $y ) >= 0; }, name => "my_function_to_test output is non-negative"; my $runner = Test::LectroTest::TestRunner->new(); $runner->run_suite( $prop_non_neg, # ... more properties here ... ); DESCRIPTION
STOP! If you're just looking for an easy way to write and run unit tests, see Test::LectroTest first. Once you're comfortable with what is presented there and ready to delve into the full offerings of properties, this is the document for you. This module allows you to define Properties that can be checked automatically by Test::LectroTest. A Property is a specification of your software's required behavior over a given set of conditions. The set of conditions is given by a generator-binding specification. The required behavior is defined implicitly by a block of code that tests your software for a given set of generated conditions; if your software matches the expected behavor, the block of code returns true; otherwise, false. This documentation serves as reference documentation for LectroTest Properties. If you don't understand the basics of Properties yet, see "OVERVIEW" in Test::LectroTest::Tutorial before continuing. Two ways to create Properties There are two ways to create a property: 1. Use the "Property" function to promote a block of code that contains both a generator-binding specification and a behavior test into a Test::LectroTest::Property object. This is the preferred method. Example: my $prop1 = Property { ##[ x <- Int ]## thing_to_test($x) >= 0; }, name => "thing_to_test is non-negative"; 2. Use the "new" method of Test::LectroTest::Property and provide it with the necessary ingredients via named parameters: my $prop2 = Test::LectroTest::Property->new( inputs => [ x => Int ], test => sub { my ($tcon,$x) = @_; thing_to_test($x) >= 0 }, name => "thing_to_test is non-negative" ); Both are equivalent, but the first is concise, easier to read, and lets LectroTest do some of the heavy lifting for you. The second is probably better, however, if you are constructing property specifications programmatically. Generator-binding specification The generator-binding specification declares that certain variables are to be bound to certain kinds of random-value generators during the tests of your software's behavior. The number and kind of generators define the "condition space" that is examined during property checks. If you use the "Property" function to create your properties, your generator-binding specification must come first in your code block, and you must use the following syntax: ##[ var1 <- gen1, var2 <- gen2, ... ]## Comments are not allowed within the specification, but you may break it across multiple lines: ##[ var1 <- gen1, var2 <- gen2, ... ]## or ##[ var1 <- gen1, var2 <- gen2, ... ]## Further, for better integration with syntax-highlighting IDEs, the terminating "]##" delimiter may be preceded by a hash symbol "#" and optional whitespace to make it appear like a comment: ##[ var1 <- gen1, var2 <- gen2, ... # ]## On the other hand, if you use "Test::LectroTest::Property->new()" to create your objects, the generator-binding specification takes the form of an array reference containing variable-generator pairs that is passed to "new()" via the parameter named "inputs": inputs => [ var1 => gen1, var2 => gen2, ... ] Normal Perl syntax applies here. Specifying multiple sets of generator bindings Sometimes you may want to repeat a property check with multiple sets of generator bindings. This can happen, for instance, when your condition space is vast and you want to ensure that a particular portion of it receives focused coverage while still sampling the overall space. For times like this, you can list multiple sets of bindings within the "##[" and "]##" delimiters, like so: ##[ var1 <- gen1A, ... ], [ var1 <- gen1B, ... ], ... more sets of bindings ... [ var1 <- gen1N, ... ]## Note that only the first and last set need the special delimiters. The equivalent when using "new()" is as follows: inputs => [ [ var1 => gen1A, ... ], [ var1 => gen1B, ... ], ... [ var1 => gen1N, ... ] ] Regardless of how you declare the sets of bindings, each set must provide bindings for the exact same set of variables. (The generators, of course, can be different.) For example, this kind of thing is illegal: ##[ x <- Int ], [ y <- Int ]## The above is illegal because both sets of bindings must use x or both must use y; they can't each use a different variable. ##[ x <- Int ], [ x <- Int, y <- Float ]## The above is illegal because the second set has an extra variable that isn't present in the first. Both sets must use exactly the same variables. None of the variables may be extra, none may be missing, and all must be named identically across the sets of bindings. Behavior test The behavior test is a subroutine that accepts a test-controller object and a given set of input conditions, tests your software's observed behavior against the required behavior with respect to the input conditions, and returns true or false to indicate acceptance or rejection. If you are using the "Property" function to create your property objects, lexically bound variables are created and loaded with values automatically, per your input-generator specification, so you can just go ahead and use the variables immediately: my $prop = Property { ##[ i <- Int, delta <- Float(range=>[0,1]) ]## my $lo_val = my_thing_to_test($i); my $hi_val = my_thing_to_test($i + $delta); $lo_val == $hi_val; }, name => "my_thing_to_test ignores fractions" ; On the other hand, if you are using "Test::LectroTest::Property->new()", you must declare and initialize these variables manually from Perl's @_ variable in lexicographically increasing order after receiving $tcon, the test controller object. (This inconvenience, by the way, is why the former method is preferred.) The hard way: my $prop = Test::LectroTest::Property->new( inputs => [ i => Int, delta => Float(range=>[0,1]) ], test => sub { my ($tcon, $delta, $i) = @_; my $lo_val = my_thing_to_test($i); my $hi_val = my_thing_to_test($i + $delta); $lo_val == $hi_val }, name => "my_thing_to_test ignores fractions" ) ; Control logic, retries, and labeling Inside the behavior test, you have access to a special variable $tcon that allows you to interact with the test controller. Through $tcon you can do the following: o retry the current trial with different inputs (if you don't like the inputs you were given at first) o add labels to the current trial for reporting purposes o attach notes and variable dumps to the current trial for diagnostic purposes, should the trial fail (For the full details of what you can do with $tcon see the "testcontroller" section of Test::LectroTest::TestRunner.) For example, let's say that we have written a function "my_sqrt" that returns the square root of its input. In order to check whether our implementation fulfills the mathematical definition of square root, we might specify the following property: my $epsilon = 0.000_001; Property { ##[ x <- Float ]## return $tcon->retry if $x < 0; $tcon->label("less than one") if $x < 1; my $sx = my_sqrt( $x ); abs($sx * $sx - $x) < $epsilon; }, name => "my_sqrt satisfies defn of square root"; Because we don't want to deal with imaginary numbers, our square-root function is defined only over non-negative numbers. To make sure we don't accidentally check our property "at" a negative number, we use the following line to re-start the trial with a different input should the input we are given at first be negative: return $tcon->retry if $x < 0; An interesting fact is that for all values x between zero and one, the square root of x is larger than x itself. Perhaps our implementation treats such values as a special case. In order to be confident that we are checking this case, we added the following line: $tcon->label("less than one") if $x < 1; In the property-check output, we can see what percentage of the trials checked this case: 1..1 ok 1 - 'my_sqrt satisfies defn of square root' (1000 attempts) # 1% less than one Trivial cases Random-input generators may create some inputs that are trivial and don't provide much testing value. To make it easy to label such cases, you can use the following from within your behavior tests: $tcon->trivial if ... ; The above is exactly equivalent to the following: $tcon->label("trivial") if ... ; SEE ALSO
Test::LectroTest::Generator describes the many generators and generator combinators that you can use to define the test or condition spaces that you want LectroTest to search for bugs. Test::LectroTest::TestRunner describes the objects that check your properties and tells you how to turn their control knobs. You'll want to look here if you're interested in customizing the testing procedure. HERE BE SOURCE FILTERS
The special syntax used to specify generator bindings relies upon a source filter (see Filter::Util::Call). If you don't want to use the syntax, you can disable the filter like so: use Test::LectroTest::Property qw( NO_FILTER ); LECTROTEST HOME
The LectroTest home is http://community.moertel.com/LectroTest. There you will find more documentation, presentations, mailing-list archives, a wiki, and other helpful LectroTest-related resources. It's also the best place to ask questions. AUTHOR
Tom Moertel (tom@moertel.com) INSPIRATION
The LectroTest project was inspired by Haskell's QuickCheck module by Koen Claessen and John Hughes: http://www.cs.chalmers.se/~rjmh/QuickCheck/. COPYRIGHT and LICENSE Copyright (c) 2004-05 by Thomas G Moertel. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.3 2007-08-30 Test::LectroTest::Property(3pm)
All times are GMT -4. The time now is 07:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy