How to add second variable in awk nested if condition?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to add second variable in awk nested if condition?
# 1  
Old 07-02-2015
How to add second variable in awk nested if condition?

Hi Gurus,

I have a command to assign value based on input value.
current condition is
Code:
"if pattern matches "case", then assign "HOLD" else "SUCC"

right now, I need to add one more condition (variable name is VAR).
the condition is "
Code:
if pattern1 matches "case", then assign "HOLD" else if pattern2 not matches "exp", then SUCC else "IA".

when I try to add second variable and try a simple nested condition
Code:
 if pattern1 matches "case", then assign "HOLD" else if pattern2 not matches "exp", then SUCC

but the code doesn't work.

Code:
$cat file
aaa_case
bbb_xxx
ccc_xxx
ddd_xxx
eee_exp

Code:
 
$/usr/xpg4/bin/awk -v DUMMY="case" -v VAR="exp" 'tolower($0)~DUMMY{print "HOLD", $0} tolower($0)!~DUMMY and $0!~VAR {print "SUCC" ,$0}' OFS="," file
ON_HOLD,aaa_case
SUCCEEDED,aaa_case
SUCCEEDED,bbb_xxx
SUCCEEDED,ccc_xxx
SUCCEEDED,ddd_xxx
SUCCEEDED,eee_exp

Could anybody take a look this below code and let me know where I am wrong.

thanks in advance.
# 2  
Old 07-02-2015
Try using && rather than and


---
And try something like:
Code:
awk -v DUMMY="case" -v VAR="exp" '{if(tolower($0)~DUMMY) s="HOLD"; else if($0~VAR) s="IA"; else s="SUCC"; print s, $0} file

Output:
Code:
HOLD aaa_case
SUCC bbb_xxx
SUCC ccc_xxx
SUCC ddd_xxx
IA eee_exp


Last edited by Scrutinizer; 07-02-2015 at 04:47 PM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-02-2015
Quote:
Originally Posted by Scrutinizer
Try using && rather than and


---
And try something like:
Code:
awk -v DUMMY="case" -v VAR="exp" '{if(tolower($0)~DUMMY) s="HOLD"; else if($0~VAR) s="IA"; else s="SUCC"; print s, $0} file

Output:
Code:
HOLD aaa_case
SUCC bbb_xxx
SUCC ccc_xxx
SUCC ddd_xxx
IA eee_exp

Hi Scrutinizer.

the code works perfect.

As always, you gave me great help. thanks you very much.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to add +1 to value based on condition in input

In the awk below I am trying to add a | that will adjust $2 in the ouput by adding +1 if the original value from file that was used in $3 had a - in it. Line 3 of file is an example of this. In my current awk I just subtract one but I am not sure how to only apply this to those values without a -.... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Shell variable as awk condition

Hi, I'm trying to automate part of a script which uses awk to grab out some lines of a log file based on certain fields matching. For example, my log file looks something like the following (but 1000s of lines): 1 Tom 123 abc 345 2 Dick 345 abc 678 3 Harry 567 abc 345 4 Tom 345 cde 345... (3 Replies)
Discussion started by: chrissycc
3 Replies

3. Shell Programming and Scripting

Nested variable allocation

I've made a number of errors with this and am trying to work a solution within the same framework. /bin/ksh for host_name in ahost1 ahost2 bhost1 bhost2 do for host_prefix in a b do if echo ${host_name} | grep -qi ${host_prefix} then if ... (1 Reply)
Discussion started by: squrcles
1 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

Problem using shell variable in awk if condition

Hi friends, I'm having a bit of a problem using shell variable in an awk if statement. Please note that i'm using -v option as listed in many forums but I still don't get it working. Here's my code. Kindly help as I've gone crazy trying to work this out :wall: #!/bin/bash -xv ... (4 Replies)
Discussion started by: vishwas.s
4 Replies

6. UNIX for Advanced & Expert Users

How to parse nested variable

Hi, I want to parse nested variable in my script. for exp- c=1 G1='0318' G2='0023' G3='3092' G4='0014' while ;do g=G$c a=$g echo "Group=$g and value=$a" c=`expr $c + 1` done final output are as - --------------------------- Group=G1 and... (4 Replies)
Discussion started by: apskaushik
4 Replies

7. Shell Programming and Scripting

Nested If condition

Hi I have a requirement to create a 2 folder based on there existance if then cd $var_name if then cd $var_name3 mv -fi *.* $var_TargetPath/$var_name/$var_name3 else mkdir -p "$var_name3" chmod 755 "$var_name3" mv -fi *.* $var_TargetPath/$var_name/$var_name3 else mkdir... (7 Replies)
Discussion started by: magesh_bala
7 Replies

8. Shell Programming and Scripting

nested double quota and white space inside variable

I have a question about nested double quotes. Any help is appreciated. Here are my commands on Mac OS. # string="Ethernet \"USB Ethernet\" \"Bluetooth DUN\" AirPort FireWire \"Bluetooth PAN\"" # echo $string Ethernet "USB Ethernet" "Bluetooth DUN" AirPort FireWire "Bluetooth PAN" #... (3 Replies)
Discussion started by: lindazhou
3 Replies

9. Shell Programming and Scripting

Enviornment Variable in B shell (I call it nested variable)

#!/bin/sh APP_ROOT_MODE1=/opt/app1.0 APP_ROOT_MODE2=/opt/app2.0 APP_ROOT=${APP_ROOT_${APP_MODE}} # enviornment variable APP_MODE will be exported in the terminal where # we run the applciation, its value is string - MODE1 or MODE2 # My intension is: # when export APP_MODE=MODE1... (4 Replies)
Discussion started by: princelinux
4 Replies

10. Shell Programming and Scripting

Variable in While Loop Nested If

Hi all, I'm having problems with the setting a variable in a nested if statement. It doesn't seem to change even if it mets the 'if' condition. My script essentially looks for a user name from the output from a kerberos command. When I find the user name, I tried to change a variable and exit... (6 Replies)
Discussion started by: geass
6 Replies
Login or Register to Ask a Question