awk if comparison with variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk if comparison with variable
# 1  
Old 01-07-2016
awk if comparison with variable

Hi , Need help, p

Code:
for value in `awk -F, '{print $1 }' ad | uniq`
do
x=$(echo $value)
echo $x
echo  `awk -F, '{if( $1 == $x) sum = sum + $8 } END{print sum}' ad`  --- not working
echo  `awk -F, '{if($1 == “MT”) sum = sum + $8 } END{print sum}' ad`  -- Working but hard coded 
done;

ad file is below


Code:
MT,AP,CDM,TTML,MUM,GS,SUCC,3
MT,AP,CDM,TTSL,AP,GS,FAIL,9
MT,AP,CDM,RCom,MAH,GS,SUCC,3
MT,AP,CDM,RTL,HP,GS,SUCC,1
MT,AP,CDM,Uni,UPE,GS,SUCC,2
MT,AP,CDM,Uni,MUM,GS,SUCC,2
TTSL,AP,GS,MT,MAH,CDM,SUCC,20
TTML,AP,GS,MT,MAH,CDM,FAIL,10
Uni,KOL,GS,MT,DEL,CDM,SUCC,100
Uni,KOL,GS,MT,HAR,CDM,SUCC,200


Last edited by Don Cragun; 01-07-2016 at 02:20 PM.. Reason: Add CODE tags.
# 2  
Old 01-07-2016
Hi,
try (not tested):
Code:
echo `awk -vMT=$x -F, '{if($1 == MT ) sum = sum + $8 } END{print sum}' ad`

Regards.
# 3  
Old 01-07-2016
Hello nadeemrafikhan,

Welcome to forums, please use code tags for commands/codes/Inputs you are showing in your posts as per forum rules. coming to your query now, in awk we have to use -v option to get the value of a variable like as follows you could use for your requirement.
Code:
x=$(echo $value)
echo `awk -F, -vX="$x" '{if( $1 == X){sum = sum + $8}} END{print sum}' ad`

Above solution I have given on basis of sample input you have shown to us, if your requirement is different then request you to please show us sample input with expected sample output, so that we could help you in a better manner, hope this helps.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 01-07-2016
It's really awesome , it works. Many Thanks Ravinder SmilieSmilie
# 5  
Old 01-07-2016
Why all those command substitutions?

Instead of:
Code:
x=$(echo $value)

use
Code:
x=$value

Instead of
Code:
echo `awk -F, -v X="$x" '{if( $1 == X){sum = sum + $8}} END{print sum}' ad`

use
Code:
awk -F, -v X="$x" '{if( $1 == X){sum = sum + $8}} END{print sum}' ad



---
AND: Instead of that shell loop with multiple calls to awk programs, try a single awk with the use of an array to replace everything:
Code:
awk -F, '{T[$1]+=$8} END{for(i in T) print i,T[i]}' ad

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable comparison in 'if' with a string.

Hi All, I want to compare the variable value with string in a shell script. my code is: for var in $packsName do if then echo $var fi done But I am getting error as: ABC : not found. Am I going wrong somwhere? please guide. Thanks. (5 Replies)
Discussion started by: AB10
5 Replies

2. Shell Programming and Scripting

awk comparison

Hello all, Probably a very simple question, I am stuck with a small part of a code: I am trying to do a comparison to get the maximum value of column 6 if columns 1, 4 and 5 of two or more rows match. Here is what I am doing: awk -F'\t' '{if ($6 > a)a=$6}END{for (i in a) print i"\t"a}' ... (4 Replies)
Discussion started by: jaysean
4 Replies

3. Shell Programming and Scripting

Comparison and editing of files using awk.(And also a possible bug in awk for loop?)

I have two files which I would like to compare and then manipulate in a way. File1: pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2: pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2... (1 Reply)
Discussion started by: linuxkid
1 Replies

4. Shell Programming and Scripting

Strange variable comparison result in awk

So, I'm making a little awk script that generates a range-based histogram of a set of numbers. I've stumbled onto a strange thing. Toward the end of the process, I have this test: if ( bindex < s ) "bindex" is the "index" of my "bin" (the array element that gets incremented whenever a... (2 Replies)
Discussion started by: treesloth
2 Replies

5. Shell Programming and Scripting

Awk multiple variable array: comparison

Foo.txt 20 40 57 50 22 51 66 26 17 15 63 18 80 46 78 99 87 2 14 14 51 47 49 100 58 Bar.txt 20 22 51 15 63 78 99 55 51 58 How to get output using awk 20 22 57 50 51 15 26 17 63 78 80 46 99 55 - - 51 58 49 100 (5 Replies)
Discussion started by: genehunter
5 Replies

6. Shell Programming and Scripting

Comparison between variable and regexp

Hi all, How I could compare variable and regexp? For example... I am running the script ./aaa.sh -b * * is variable $2 (second argument of script). I would compare variables $a (generated from my cycle) and $2 as regexp. I need from regular expression find file that satisfies this regexp.... (1 Reply)
Discussion started by: Koblenc
1 Replies

7. Shell Programming and Scripting

Variable comparison

Can anyone help me with this section of code? The scenario is a value drops from A to B or A to C or B to C. If it drops from A to B or B to C, we print "Drop one level" If it drops from A to C, we print "Dropped two levels". The problem is script is throwing error when comparing variable... (2 Replies)
Discussion started by: sundar63
2 Replies

8. Shell Programming and Scripting

awk comparison with variable

hi, I want to compare i variable in the awk statement but its not working out. Pl help me out If we do the comparison like this its OK, cat sample | awk -F" ", '{if ($1=="1-Sep-2009") print $1,$2,$3,$4,$5}' But if u use a variable instead of "1-Sept-2009", it does not return anything,... (2 Replies)
Discussion started by: asadlone
2 Replies

9. UNIX for Dummies Questions & Answers

multiple comparison in awk

I have an input file. Each line in it has several characters. If the first three characters of the line is '000' or '001' or '002' or '003', I need to print it in output. How can I do this in awk. I am able to do if the search string is only one (let us say 000). cat <filename> | awk... (1 Reply)
Discussion started by: paruthiveeran
1 Replies

10. Shell Programming and Scripting

variable comparison

Hello, I am trying to compare two variables, which both have a word stored in it. The code I am using is. Please tell me which one of these is correct.Or please tell me the correct syntax. Thankyou if then if then if then if then if then None of them seems to work... (3 Replies)
Discussion started by: Freakhan
3 Replies
Login or Register to Ask a Question