The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Count first column on the basis of two other columns kaustubh137 Shell Programming and Scripting 1 05-29-2008 08:54 AM
How to get the count only if two column matches? gobinath Shell Programming and Scripting 3 05-27-2008 04:01 AM
nawk-how count the number of occurances of a pattern, when don't know the pattern cyber111 Shell Programming and Scripting 2 05-11-2008 03:00 AM
Count of matched pattern occurance palash2k UNIX for Dummies Questions & Answers 3 04-24-2008 03:33 PM
Max column count in a file gemini Shell Programming and Scripting 2 08-24-2004 12:12 PM

Closed Thread
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 12-03-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
How to count pattern in column

Hi,

I have another problem here on Bash. Assume I have an output file containing two columns which the second one contains :-

abc`dgdf`sdfdg`
dfgfg```ssd`


I would like to count the "`" characters for each line (which is first column). I am wondering what command can I used other than AWK to calculate these characters for each line.

I tried to use AWK by using if ($2)=="`", count[$1]=count++; however, it doesnt work.

Please advise. Thanks.


-Jason
  #2 (permalink)  
Old 12-03-2007
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
Code:
awk 'BEGIN {FS="`"} {print $1, NF-1}' file
Regards
  #3 (permalink)  
Old 12-03-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Hi franklin,

Thanks for your reply. It works well, but I need to know more about this command. From the Field Separator "`", does that means NF is actually referring to the field separator since we have set only one in the BEGIN loop?

If that is so, if i have other field separator let say '&' or '%' etc..how could I differentiate them using NF since NF to my understanding is referring to number of fields.

Please advise. Thanks.


Rgrds,
Jason
  #4 (permalink)  
Old 12-04-2007
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
The default fieldseperator of awk is a tab or spaces, which is change here with the BEGIN statement.
The fieldseperator is used as a trick to count the character.
NF is the number of fields of each row so if you have 5 fields there will be 4 fieldseperators.

Regards
  #5 (permalink)  
Old 12-04-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Hi Franklin,

I agree with what you explained. But given the fact if I have one line consist of different patterns such as "`" (the one you showed me) and "$".
or even more other patterns,

If that is the case how can I use NF to differentiate these characters.

For your information, my input is a line of statements which is something like below:-

halo`world$$``

If using the method you showed me, we could have one NF tricked one at a time. How could we represent for both patterns in this case.

Please advise. Hope you could get my meaning. Thanks.


-Jason
  #6 (permalink)  
Old 12-04-2007
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,294
You can't use more than one fieldseparator in this case, for more characters you should use another method.

Regards
  #7 (permalink)  
Old 12-04-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by ahjiefreak View Post
Hi Franklin,

I agree with what you explained. But given the fact if I have one line consist of different patterns such as "`" (the one you showed me) and "$".
or even more other patterns,

If that is the case how can I use NF to differentiate these characters.

For your information, my input is a line of statements which is something like below:-

halo`world$$``
Code:
awk -F'[`$]' ' {print $1, NF-1}' file
Closed Thread

Bookmarks

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 08:11 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