Pattern Matching Count Urgent


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pattern Matching Count Urgent
# 1  
Old 07-30-2008
Question Pattern Matching Count Urgent

The input is like

NO Code
030019991 22
030087819 0
030089001 22
030178901 39
030333333 22

Patterns i want to match is 0300 0301 0303. i want to count different Codes occurances for each of these pattern. for example for 0300 Code 22 occurs 2 times and 0 occurs 1 time. output should be like that

300:
22 2
0 1
0301:
39 1 etc
I have large no of patterns so i have to save them in array and then match them . Please can someone provide with the shell script urgently.
# 2  
Old 07-30-2008
Hammer & Screwdriver Perhaps a start

The following is based on the small sample file provided:

Code:
> cat patt_file | cut -c1-4,10-12 | sort | uniq -c
      1 0300 0
      2 0300 22
      1 0301 39
      1 0303 22

You might need to play with the layout, but the output is
count pattern code
# 3  
Old 07-30-2008
Works But

Quote:
Originally Posted by joeyg
The following is based on the small sample file provided:

Code:
> cat patt_file | cut -c1-4,10-12 | sort | uniq -c
      1 0300 0
      2 0300 22
      1 0301 39
      1 0303 22

You might need to play with the layout, but the output is
count pattern code
it works fine but matching Patterns may not be 4 digit in lenght that may be 2,5 or 7 etc. Alse file is csv and much more colums may be present between code 22 etc and No 0300... . I will much appreciate your help
# 4  
Old 07-30-2008
Question If file is csv, then perhaps easier

Can you include a sample of the datafile? Perhaps first ten lines or so?

Since you say csv, then you can cut by field rather than character positions - thereby addressing your concern about field length.
# 5  
Old 07-31-2008
Another Problem

Quote:
Originally Posted by joeyg
Can you include a sample of the datafile? Perhaps first ten lines or so?

Since you say csv, then you can cut by field rather than character positions - thereby addressing your concern about field length.
i have written the script in cshell but there is another problem that when define variables in a file then run the file error ocurrs something like
missing )
e:g >>cat abc
set a =0;
set xyz =(1 2 3 4 5 6);
>> chmod 777 abc;
>>abc
missing parameter )
how can i correct this. I will much appreciate your help.
# 6  
Old 07-31-2008
You are not defining 'pattern' - you gave an example of four digits out of eight at the start of a line.

Assuming the first column is a pattern - any combination of digits/letters:
Code:
awk '{ arr[$1]++ }
       END { for (i in arr) { print i, arr[i]} } ' filename

# 7  
Old 07-31-2008
How to define variables in a file

ok thnk

Last edited by krabu; 10-10-2008 at 04:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

2. 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

3. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

4. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

5. 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

6. Shell Programming and Scripting

Multiple pattern matching using awk and getting count of lines

Hi , I have a file which has multiple rows of data, i want to match the pattern for two columns and if both conditions satisfied i have to add the counter by 1 and finally print the count value. How to proceed... I tried in this way... awk -F, 'BEGIN {cnt = 0} {if $6 == "VLY278" &&... (6 Replies)
Discussion started by: aemunathan
6 Replies

7. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

8. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

9. 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

10. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies
Login or Register to Ask a Question