Sponsored Content
Full Discussion: awk comparing variables
Top Forums Shell Programming and Scripting awk comparing variables Post 302613409 by Jikuu on Tuesday 27th of March 2012 07:47:30 AM
Old 03-27-2012
and if i wanted multiple variables?

Code:
VAR1=Bob
VAR2=Steve

awk -v VAR1=$VAR1 VAR2=$VAR2 '($3 == VAR1 && $4 == VAR2) { print $1 }' file.txt

I know the '($3 == VAR1 && $4 == VAR2)' bit is right but I'm not too sure on declaring multiple variables. What separator would be used?
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

comparing variables

I have searched and found a few threads that have dealt with this, but the examples I've tried haven't seemed to help. I am monitoring our database log for high checkpoints. I can parse out the checkpoint value which can be anywhere from zero into a 3 digit number. I set a variable to be the... (3 Replies)
Discussion started by: MizzGail
3 Replies

2. Shell Programming and Scripting

Comparing two variables

Script #!/bin/sh hardware=PC os=WindowsNET for i in `cat newservers` do x=`sudo /opt/openv/netbackup/bin/admincmd/bpplclients |grep $i |head -40 |grep $i|awk '{print $3;exit}'` if then echo "$i is already added" else echo "Need to add" fi done O/p in debug mode bash-2.05$... (3 Replies)
Discussion started by: rajip23
3 Replies

3. Shell Programming and Scripting

Comparing Variables in Perl

Hi. I have three arrays. @a=('AB','CD','EF'); @b=('AB,'DG',HK'); @c=('DD','TT','MM'); I want to compare the elements of the first two array and if they match then so some substition. I tried using the if statement using the scalar value of the array but its not giving me any output. ... (7 Replies)
Discussion started by: kamitsin
7 Replies

4. UNIX for Dummies Questions & Answers

comparing variables

im trying to compare ipaddresses. i loop through an array to see if the ip is already is in the array and if it is it should set a flag and then i wont add it to the array. but its just adding all the ipaddresses to the array if ] then ... (3 Replies)
Discussion started by: magnia
3 Replies

5. Shell Programming and Scripting

comparing variables in awk array

Hi, I am trying to sort and display the below(like) input using awk command: Input: ------ 0;A 4;A 5;A 33;A 45;A 0;B 4;B 5;B 33;B 45;B Output (desired): (5 Replies)
Discussion started by: pvamsikr
5 Replies

6. Shell Programming and Scripting

Comparing variables in awk

I'm writing a shellscript that monitors the price of a watch. If the prices changes, it should email me. The body of the email will show the old price and the new price. However when I compare the two awk variables(oldprice and newprice) it always says they're not the same. The shellscript goes out... (2 Replies)
Discussion started by: Shinsuio
2 Replies

7. Shell Programming and Scripting

Comparing multiple variables

Hi! I've come up with a ksh-script that produces one or more lists of hosts. At the and of the script, I would like to print only those hosts that exists in all the lists. Ex. HOSTS="host1 host2 host3 host11" HOSTS="host1 host2 host4" HOSTS="host2 host11" HOSTS="host2 host5 host6 host7... (1 Reply)
Discussion started by: Bugenhagen
1 Replies

8. Shell Programming and Scripting

comparing multiple variables by 'if then'

Hi, I am a noob at shell scripting. basically I am trying to compare row counts from 8 tables in different databases. I have managed to get the row counts using awk from the spool files for both databases. now I have 16 variables with me for database 1 : $A $B $C $D $E $F $G... (3 Replies)
Discussion started by: smallville
3 Replies

9. Shell Programming and Scripting

Comparing content of two variables

I have very abstract need of "comparing two variables" and take subsequent actions. please refer to image below https://lh3.googleusercontent.com/-frNk5iA3q1c/TjI3lE0sWOI/AAAAAAAAAIE/fxzB1w07gas/script_block.JPG I have a part of script which reads a file and generates variables based on... (4 Replies)
Discussion started by: animesharma
4 Replies

10. Shell Programming and Scripting

Comparing two variables

I have a script like this. Just couldn't get the comparison part work. Any thought? thanks, #!/usr/bin/ksh -x STEP=`echo $(basename $0 .ksh) | tr "" ""` log=/skip.log while read LINE do if then echo `date`: STEP $STEP skipped by user >> $log exit 0 fi done < $1 echo... (0 Replies)
Discussion started by: ghostmic
0 Replies
General::Extended(3pm)					User Contributed Perl Documentation				    General::Extended(3pm)

NAME
Config::General::Extended - Extended access to Config files SYNOPSIS
use Config::General; $conf = new Config::General( -ConfigFile => 'configfile', -ExtendedAccess => 1 ); DESCRIPTION
This is an internal module which makes it possible to use object oriented methods to access parts of your config file. Normally you don't call it directly. METHODS
configfile('filename') Set the filename to be used by save to "filename". It returns the current configured filename if called without arguments. obj('key') Returns a new object (of Config::General::Extended Class) from the given key. Short example: Assume you have the following config: <individual> <martin> age 23 </martin> <joseph> age 56 </joseph> </individual> <other> blah blubber blah gobble leer </other> and already read it in using Config::General::Extended::new(), then you can get a new object from the "individual" block this way: $individual = $conf->obj("individual"); Now if you call getall on $individual (just for reference) you would get: $VAR1 = ( martin => { age => 13 } ); Or, here is another use: my $individual = $conf->obj("individual"); foreach my $person ($conf->keys("individual")) { $man = $individual->obj($person); print "$person is " . $man->value("age") . " years old "; } See the discussion on hash() and value() below. If the key from which you want to create a new object is empty, an empty object will be returned. If you run the following on the above config: $obj = $conf->obj("other")->obj("leer"); Then $obj will be empty, just like if you have had run this: $obj = new Config::General::Extended( () ); Read operations on this empty object will return nothing or even fail. But you can use an empty object for creating a new config using write operations, i.e.: $obj->someoption("value"); See the discussion on AUTOLOAD METHODS below. If the key points to a list of hashes, a list of objects will be returned. Given the following example config: <option> name = max </option> <option> name = bea </option> you could write code like this to access the list the OOP way: my $objlist = $conf->obj("option"); foreach my $option (@{$objlist}) { print $option->name; } Please note that the list will be returned as a reference to an array. Empty elements or non-hash elements of the list, if any, will be skipped. hash('key') This method returns a hash(if it is one!) from the config which is referenced by "key". Given the sample config above you would get: my %sub_hash = $conf->hash("individual"); print Dumper(\%sub_hash); $VAR1 = { martin => { age => 13 } }; array('key') This the equivalent of hash() mentioned above, except that it returns an array. Again, we use the sample config mentioned above: $other = $conf->obj("other"); my @blahs = $other->array("blah"); print Dumper(@blahs); $VAR1 = [ "blubber", "gobble" ]; value('key') This method returns the scalar value of a given key. Given the following sample config: name = arthur age = 23 you could do something like that: print $conf->value("name") . " is " . $conf->value("age") . " years old "; You can use this method also to set the value of "key" to something if you give over a hash reference, array reference or a scalar in addition to the key. An example: $conf->value("key", \%somehash); # or $conf->value("key", @somearray); # or $conf->value("key", $somescalar); Please note, that this method does not complain about existing values within "key"! is_hash('key') is_array('key') is_scalar('key') As seen above, you can access parts of your current config using hash, array or scalar methods. But you are right if you guess, that this might become problematic, if for example you call hash() on a key which is in real not a hash but a scalar. Under normal circumstances perl would refuse this and die. To avoid such behavior you can use one of the methods is_hash() is_array() is_scalar() to check if the value of "key" is really what you expect it to be. An example(based on the config example from above): if($conf->is_hash("individual") { $individual = $conf->obj("individual"); } else { die "You need to configure a "individual" block! "; } exists('key') This method returns just true if the given key exists in the config. keys('key') Returns an array of the keys under the specified "key". If you use the example config above you yould do that: print Dumper($conf->keys("individual"); $VAR1 = [ "martin", "joseph" ]; If no key name was supplied, then the keys of the object itself will be returned. You can use this method in foreach loops as seen in an example above(obj() ). delete ('key') This method removes the given key and all associated data from the internal hash structure. If 'key' contained data, then this data will be returned, otherwise undef will be returned. AUTOLOAD METHODS
Another usefull feature is implemented in this class using the AUTOLOAD feature of perl. If you know the keynames of a block within your config, you can access to the values of each individual key using the method notation. See the following example and you will get it: We assume the following config: <person> name = Moser prename = Peter birth = 12.10.1972 </person> Now we read it in and process it: my $conf = new Config::General::Extended("configfile"); my $person = $conf->obj("person"); print $person->prename . " " . $person->name . " is " . $person->age . " years old "; This notation supports only scalar values! You need to make sure, that the block <person> does not contain any subblock or multiple identical options(which will become an array after parsing)! If you access a non-existent key this way, Config::General will croak an error. You can turn this behavior off by setting -StrictObjects to 0 or "no". In this case undef will be returned. Of course you can use this kind of methods for writing data too: $person->name("Neustein"); This changes the value of the "name" key to "Neustein". This feature behaves exactly like value(), which means you can assign hash or array references as well and that existing values under the given key will be overwritten. COPYRIGHT
Copyright (c) 2000-2010 Thomas Linden This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. BUGS
none known yet. AUTHOR
Thomas Linden <tlinden |AT| cpan.org> VERSION
2.05 perl v5.10.1 2010-12-01 General::Extended(3pm)
All times are GMT -4. The time now is 11:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy