count the number of occurring patterns in a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count the number of occurring patterns in a file.
# 1  
Old 05-25-2012
count the number of occurring patterns in a file.

Hi,

I have a file with a '|' pipe delimeter. I want to find number of counts for a particular pattern in particular field. Is it possible to do it in a single command?

1) want to find total number of "0" in field 4.
2) want to find total number of different records in field 4 ( similar to group by in oracle)

data
Code:
AAAAA|BBBBB|CCCCC|0|USA
AAAa|BBBBB|ccccc|0|CAN
AAAAA|BBBBB|CCCCC|0|USA
AAAAA|BBBBB|CCCCC|1|USA
AAAAA|BBBBB|CCCCC|0|USA
AAAAA|BBBBB|CCCCC|2|USA
AAAAA|BBBBB|CCCCC|0|USA
AAAAA|BBBBB|CCCCC|0|USA
AAAAA|BBBBB|CCCCC|1|USA

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 05-25-2012 at 10:56 AM.. Reason: fixed code tags
# 2  
Old 05-25-2012
Something like this?
Code:
awk -F"|" '{a[$4]++}END{for(i in a)print i, a[i]}' file

# 3  
Old 05-25-2012
Yes this looks correct, but it is possible to have spacing between the counts and the patterns. They seem to be concatenating

---------- Post updated at 10:24 AM ---------- Previous update was at 10:17 AM ----------

How about
1) want to find total number of "0" in field 4.
# 4  
Old 05-25-2012
Quote:
Originally Posted by rudoraj
Yes this looks correct, but it is possible to have spacing between the counts and the patterns. They seem to be concatenating
Strange... try this:
Code:
awk -F"|" '{a[$4]++}END{for(i in a)print i "," a[i]}' file

# 5  
Old 05-31-2012
Code:
awk -F"|" '{a[$14]++}END{for(i in a)print a[i] i }' file_nm

The above command works fine.
I wanted to extend it to see if all the records from field 14, if they are 0, then extract it to a new file.

example

Code:
1|2|3|4|5|6|7|8|9|10|11|12|13|14|15
A|B|C|D|E|F|G|H|I|J|K|L|M|N|O
A|B|C|D|E|F|G|H|I|J|K|L|M|0|O
A|B|C|D|E|F|G|H|I|J|K|L|M|0|O
A|B|C|D|E|F|G|H|I|J|K|L|M|100|O
A|B|C|D|E|F|G|H|I|J|K|L|M|200|O
A|B|C|D|E|F|G|H|I|J|K|L|M|1|O


Results.
Only the following records should be extracted to a new file
Code:
A|B|C|D|E|F|G|H|I|J|K|L|M|0|O
A|B|C|D|E|F|G|H|I|J|K|L|M|0|O


Thank,s

Last edited by Scrutinizer; 05-31-2012 at 11:16 AM.. Reason: code tags
# 6  
Old 06-01-2012
Code:
awk -F"|" '$14==0' file > newfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

2. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

3. Shell Programming and Scripting

How to count number of files in directory and write to new file with number of files and their name?

Hi! I just want to count number of files in a directory, and write to new text file, with number of files and their name output should look like this,, assume that below one is a new file created by script Number of files in directory = 25 1. a.txt 2. abc.txt 3. asd.dat... (20 Replies)
Discussion started by: Akshay Hegde
20 Replies

4. Shell Programming and Scripting

Awk to Count Multiple patterns in a huge file

Hi, I have a file that is 430K lines long. It has records like below |site1|MAP |site2|MAP |site1|MODAL |site2|MAP |site2|MODAL |site2|LINK |site1|LINK My task is to count the number of time MAP, MODAL, LINK occurs for a single site and write new records like below to a new file ... (5 Replies)
Discussion started by: reach.sree@gmai
5 Replies

5. UNIX for Advanced & Expert Users

Count number of unique patterns from a log file

Hello Everyone I need your help in fixing this issue., I have a log file which has data of users logging in to an application. I want to search for a particular pattern in the log ISSessionValidated=N If this key word is found , the above 8 lines will contain the name of the user who's... (12 Replies)
Discussion started by: xtechkid
12 Replies

6. UNIX for Dummies Questions & Answers

Count of Number of Lines in a File

Dear Members, I want to count the number of lines in a file; for that i am using the following command : FILE_LINE_COUNT=`wc -l $INT_IN/$RAW_FILE_NAME` if i do an echo on FILE_LINE_COUNT then i get 241 /home/data/testfile.txt I don't want the directory path to be displayed. Variable... (1 Reply)
Discussion started by: sandeep_1105
1 Replies

7. Shell Programming and Scripting

Count lines between two patterns inside a file

Hi, Im doing a script to find the number of lines included inside a file newly. These lines are in between #ifdef FLAG1 and #else or #endif or #else and #endif. I tried like this, awk '/#ifdef Flag1/,/#e/{print}' aa.c | wc -l awk '/#ifndef Flag1/,/#endif/{print}' aa.c | awk... (6 Replies)
Discussion started by: priyadarshini
6 Replies

8. Shell Programming and Scripting

File number count

hi all i want to count the number of files in a particlar dir i have wrote the codes as below fileCount= ls | wc -l if then echo " " elif then echo " " fi however when i excute the program there are error happens " unary operator expected" i just woundering fileCount=... (4 Replies)
Discussion started by: cryogen
4 Replies

9. UNIX for Dummies Questions & Answers

How to count number of delimiters in a file name

I have a list of files with names as "FULL_abcd_xyz_timestamp.txt" and "FULL_xx_abcd_xyz_timestamp.txt". I am writing a script with a 'for loop' to take each file, strip the "FULL" and "timestamp" from the file name and do some actions on the contains of the file. So I need to know the number of... (4 Replies)
Discussion started by: ayanbiswas
4 Replies

10. Shell Programming and Scripting

Display most top 10 occurring words along with number of ocurences of word inthe text

I need Display the most top 10 occurring words along with the number of occurences of those words in the given text. Sample text as below: "The Travails of Single South Indian men of conservative upbringing" or "Why we don't get any..." Yet another action packed weekend in Mumbai, full of... (2 Replies)
Discussion started by: smacherla
2 Replies
Login or Register to Ask a Question