AWK matching problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK matching problem
# 1  
Old 10-22-2009
AWK matching problem

hi

i have a list as follow:
Quote:
Taba 4
aba 1
Taba 2
aba 1
aba 0
where "aba" can be zero or one. I want match all "aba" and count how many are set to one. I mean in the previous case i should get just "2":

I tried as follow:

Code:
awk '/^aba/ BEGIN{i=0}{if($2==1) i=i+1}END{print i}' file

but i get some errors.
If i do:

Code:
awk '/^aba/ {print $0}' do

I get the list with all "aba " i think that the problem refers to the BEGIN. Can anyone suggest a solution?

Thanks

D.
# 2  
Old 10-22-2009
Code:
awk '$1 ~ /^aba/  && $2 ~ /1/ {i = i+1} END {print i}' a.txt

or

Code:
awk '$1 == "aba"  && $2 == "1" {i = i+1} END {print i}' a.txt

# 3  
Old 10-22-2009
to be more precise in case you have things like "abalone 1" , use equality
Code:
awk '$1=="aba" && $2==1{c++}END{print c}' file

# 4  
Old 10-22-2009
great!
thanks for both solutions

D.

EDIT: i found out also that in this way works:

Code:
awk '/^aba/ {if($2==1) i=i+1}END{print i}' file

Smilie

Last edited by Dedalus; 10-22-2009 at 11:57 AM..
# 5  
Old 10-22-2009
Code:
awk '$0=="aba 1"{i++}END{print i}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Pattern matching problem

if i have to do pattern match for file name with digit alphanumeric value like this File_1234.csv File_12sd45rg.csv i am using this File_*.csv and File_*.csv for digit pattern match. when i am doing pattern match for the digit then both alphanumeric match and digit match is coming. ... (3 Replies)
Discussion started by: ramsavi
3 Replies

3. Shell Programming and Scripting

Pattern matching problem

Hi I need a bash script that can search through a text file for all lines starting with 71502FSC1206 on every line it finds starting with this I need to place a letter F at the 127 position on that line. Thanks Paul (6 Replies)
Discussion started by: firefox2k2
6 Replies

4. Shell Programming and Scripting

awk: matching and not matching

Hello all, simple matching and if not match problem that i can't figure out. file1 hostname: 30 10 * * * /home/toto/start PROD instance_name1 -p 00 9 * * * /home/toto/start PROD instance_name2 -p 15 8 * * * /home/toto/start PROD instance_name3 -p hostname2: 00 8 * * *... (5 Replies)
Discussion started by: maverick72
5 Replies

5. Shell Programming and Scripting

pattern matching problem

# cat email.txt | grep -i "To:" To: <test@example.com> # cat email.txt | grep -i "Subject" Subject: Test Subject: How are you. I need to print only test@example.com from To field need to eliminate "< & >" from To field and need to print entire subject after Subject: It should be #... (7 Replies)
Discussion started by: mirfan
7 Replies

6. Shell Programming and Scripting

awk pattern matching problem -not recognizing a column

Hi all, I am new to awk. I want to print the line numbers if the column has a particular value. For example I have: cat FILE1 COL1 COL2 X114 0 X116 0 X117 0 X120 0 X121 0 X125 0 X126 0 X127 0 X131 1 X132 0 X135 0 X136 0 (3 Replies)
Discussion started by: newpro
3 Replies

7. Shell Programming and Scripting

awk BEGIN END and string matching problem

Hi, Contents of BBS-list file: foo foo foo awk ' BEGIN { print "Analysis of \"foo\"" } /foo/ { ++n } END { print "\"foo\" appears", n, "times." }' BBS-list Output: Analysis of "foo" "foo" appears 3 times. awk ' (3 Replies)
Discussion started by: cola
3 Replies

8. Shell Programming and Scripting

matching file problem

hi I need help for matching two files a.txt 123,20080921,2419,ec1,20081021 456,20080923,2420,ec1,20081021 789,20080920,2419,ec1,20081021 124,20080921,2421,ec1,20081023 125,20080921,2419,ec1,20081021 126,20080921,2419,ec1,20081021 128,20080921,2419,ec1,20081021 b.txt ... (2 Replies)
Discussion started by: anish19
2 Replies

9. Shell Programming and Scripting

pattern matching problem

FilesToBackup='*.track* *.xml *.vm* *.gz Trace* TRACE* "*core*" *.out fcif_data_* esi_error_* *.rollback *.sed R.* APStatus_* log* *.output* send_mail* downenv* check_env* intaspurge_db_* sqlnet.log *.rpt *.html *.csv "*TSC*"' and i am using it like this- echo Moving files from $(pwd): ... (2 Replies)
Discussion started by: namishtiwari
2 Replies

10. Shell Programming and Scripting

Problem with pattren Matching

I have a set of programs and there coressponding MAPSETs i tried grep on the all the programs and got the following out put from this i want to extract only the Program Name and Mapset name {i.e. the word in (' ') after MAPSET } There or some cases where u have no ( after MAPSET that need... (7 Replies)
Discussion started by: pbsrinivas
7 Replies
Login or Register to Ask a Question