The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
problem using sed for pattern matching lassimanji Shell Programming and Scripting 5 04-22-2009 05:42 PM
matching file problem anish19 Shell Programming and Scripting 2 07-23-2008 12:04 PM
pattern matching problem namishtiwari Shell Programming and Scripting 2 05-23-2008 08:33 AM
pattern matching problem rein Shell Programming and Scripting 8 10-27-2007 12:44 AM
Problem with pattren Matching pbsrinivas Shell Programming and Scripting 7 11-27-2006 04:17 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-22-2009
Dedalus Dedalus is offline
Registered User
  
 

Join Date: May 2009
Location: Italy/France
Posts: 60
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 (permalink)  
Old 10-22-2009
Amit.Sagpariya Amit.Sagpariya is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 111

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 (permalink)  
Old 10-22-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,556
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 (permalink)  
Old 10-22-2009
Dedalus Dedalus is offline
Registered User
  
 

Join Date: May 2009
Location: Italy/France
Posts: 60
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


Last edited by Dedalus; 10-22-2009 at 11:57 AM..
  #5 (permalink)  
Old 10-22-2009
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,440

Code:
awk '$0=="aba 1"{i++}END{print i}' file

Reply

Bookmarks

Tags
awk, match

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0