How to count pattern in column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to count pattern in column
# 1  
Old 12-03-2007
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  
Old 12-03-2007
Code:
awk 'BEGIN {FS="`"} {print $1, NF-1}' file

Regards
# 3  
Old 12-03-2007
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  
Old 12-04-2007
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  
Old 12-04-2007
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  
Old 12-04-2007
You can't use more than one fieldseparator in this case, for more characters you should use another method.

Regards
# 7  
Old 12-04-2007
Quote:
Originally Posted by ahjiefreak
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If pattern in column 3 matches pattern in column 2 (any row), print value in column 1

Hi all, I have searched and searched, but I have not found a solution that quite fits what I am trying to do. I have a long list of data in three columns. Below is a sample: 1,10,8 2,12,10 3,13,12 4,14,14 5,15,16 6,16,18 Please use code tags What I need to do is as follows: If a... (4 Replies)
Discussion started by: bleedingturnip
4 Replies

2. Shell Programming and Scripting

Column 2 string count in Column 3

My I/p is Col1|Col2|Col3 2116209997932|POSIX INC|POSIX 2116209997933|POSIX INC|POSIX 2116210089479|POSIX INC|POSIX 2116210180502|POSIX INC|POSIX 2116210512279|POSIX INC|Aero 2116210516838|POSIX INC|POSIX 2116210534342|POSIX INC|postal 2116210534345|POSIX INC|postal ... (6 Replies)
Discussion started by: nikhil jain
6 Replies

3. Shell Programming and Scripting

Read first column and count lines in second column using awk

Hello all, I would like to ask your help here: I've a huge file that has 2 columns. A part of it is: sorted.txt: kss23 rml.67lkj kss23 zhh.6gf kss23 nhd.09.fdd kss23 hp.767.88.89 fl67 nmdsfs.56.df.67 fl67 kk.fgf.98.56.n fl67 bgdgdfg.hjj.879.d fl66 kl..hfh.76.ghg fl66... (5 Replies)
Discussion started by: Padavan
5 Replies

4. Shell Programming and Scripting

Identify file pattern, take count of pattern, then act

Guys - Need your ideas on a section of code to finish something up. To make a long story short, I'm parsing a print output file that goes to pre-printed forms. I'm intercepting it, parsing it, formatting it, cutting it up into individual pages, grabbing the text I want in zones, building an... (3 Replies)
Discussion started by: ampsys
3 Replies

5. Shell Programming and Scripting

awk pattern match and count unique in column

Hi all I have a need of searching some pattern in file by month and then count unique records D11 G11 R11 -------> Pattern available in file S11 Jan$1 to $5 column contains some records in which I want to find unique for this purpose I have written script like below awk '/Jan/ ||... (4 Replies)
Discussion started by: nex_asp
4 Replies

6. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

7. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

8. Shell Programming and Scripting

need to remove duplicates based on key in first column and pattern in last column

Given a file such as this I need to remove the duplicates. 00060011 PAUL BOWSTEIN ad_waq3_921_20100826_010517.txt 00060011 PAUL BOWSTEIN ad_waq3_921_20100827_010528.txt 0624-01 RUT CORPORATION ad_sade3_10_20100827_010528.txt 0624-01 RUT CORPORATION ... (13 Replies)
Discussion started by: script_op2a
13 Replies

9. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

10. Shell Programming and Scripting

nawk-how count the number of occurances of a pattern, when don't know the pattern

I've written a script to count the total size of SAN storage LUNs, and also display the LUN sizes. From server to server, the LUNs sizes differ. What I want to do is count the occurances as they occur and change. These are the LUN sizes: 49.95 49.95 49.95 49.95 49.95 49.95 49.95 49.95... (2 Replies)
Discussion started by: cyber111
2 Replies
Login or Register to Ask a Question