Multiple pattern matching using awk and getting count of lines

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple pattern matching using awk and getting count of lines
# 1  
Old 04-17-2012
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" && substr($3,1,10) ~ /2012-04-15/  {cnt=cnt+1} } END {print cnt}' /tmp/p13_1.txt

but its not successful.
sample data is
PLI,*,2012-04-14 13:06:22,MG81-1334491568577766,1331,VLY278,ws_mob,NULL,33751059665,22377591192,22377591192,61,47,0,16,2012,04,15,1 3,06,2237,MALI PROPER ,1,2,NULL,0.171000,NULL,1,NULL,NULL,1,NULL,0,0.0
# 2  
Old 04-17-2012
Try this:

if requires () around expression so correct syntax for your solution.
awk -F, 'BEGIN {cnt = 0} {if($6 == "VLY278" && substr($3,1,10) ~ /2012-04-15/)  {cnt=cnt+1} } END {print cnt}' /tmp/p13_1.txt

Below is optimised version (date in data is 14 not 15)
awk -F, '$6=="VLY278" && $3 ~ /^2012-04-14*/ {cnt++} END {print cnt}' infile

# 3  
Old 04-17-2012

Thanks for the response....
can i use or condition as well in that

awk -F, '$6=="VLY278" || $6 == "VLY280" || $6 == "VLY366" || $6 == "TLY340" && $3 ~ /^2012-04-14*/ {cnt++} END {print cnt}' infile

# 4  
Old 04-17-2012
Yes, but it's probably best to use brackets around the or conditions to avoid confusion with operator precedence:

awk -F, '($6=="VLY278" || $6 == "VLY280" || $6 == "VLY366" || $6 == "TLY340") && $3 ~ /^2012-04-14*/ {cnt++} END {print cnt}' infile


awk -F, '$6 ~ /VLY278|VLY280|VLY366|TLY340/ && $3 ~ /^2012-04-14*/ {cnt++} END {print cnt}' infile

# 5  
Old 04-17-2012

The ouput contains very huge count value as compared to the actually expected output....

What could be the problem?
# 6  
Old 04-17-2012
Why not change cnt++ to print, it will then output each line that matches and this should help work out what it's matching.

You can also pipe this output to wc -l to confirm the count of matching lines.
# 7  
Old 04-17-2012
This part is wrong:

$3 ~ /^2012-04-14*/

You are using regex, not a shell glob. Smilie That would match 2012-04-1, since the 4 may be matched 0+ times. Drop the asterisks. or replace it with a space.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Group Multiple Lines on SINGLE line matching pattern

Hi Guys, I am trying to format my csv file. When I spool the file using sqlplus the single row output is wrapped on three lines. Somehow I managed to format that file and finally i am trying to make the multiple line on single line. The below command is working fine but I need to pass the... (3 Replies)
Discussion started by: RJSKR28
3 Replies

2. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

3. Shell Programming and Scripting

UNIX awk pattern matching and printing lines

I have the below plain text file where i have some result, in order to mail that result in html table format I have written the below script and its working well. cat result.txt Page 2015-01-01 2000 Colors 2015-02-01 3000 Landing 2015-03-02 4000 #!/bin/sh LOG=/tmp/maillog.txt... (1 Reply)
Discussion started by: close2jay
1 Replies

4. Shell Programming and Scripting

awk script issue redirecting to multiple files after matching pattern

Hi All I am having one awk and sed requirement for the below problem. I tried multiple options in my sed or awk and right output is not coming out. Problem Description ############################################################### I am having a big file say file having repeated... (4 Replies)
Discussion started by: kshitij
4 Replies

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

6. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

7. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

8. Shell Programming and Scripting

Sed/awk/perl command to replace pattern in multiple lines

Hi I know sed and awk has options to give range of line numbers, but I need to replace pattern in specific lines Something like sed -e '1s,14s,26s/pattern/new pattern/' file name Can somebody help me in this.... I am fine with see/awk/perl Thank you in advance (9 Replies)
Discussion started by: dani777
9 Replies

9. Shell Programming and Scripting

sed/awk to insert multiple lines before pattern

I'm attempting to insert multiple lines before a line matching a given search pattern. These lines are generated in a separate function and can either be piped in as stdout or read from a temporary file. I've been able to insert the lines from a file after the pattern using: sed -i '/pattern/... (2 Replies)
Discussion started by: zksailor534
2 Replies

10. Shell Programming and Scripting

pattern matching over multiple lines and deleting the first

I've got a longish log file with content such as Uplink traffic: Downlink traffic: I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in... (7 Replies)
Discussion started by: Yorkie99
7 Replies
Login or Register to Ask a Question

Featured Tech Videos