Quick awk question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick awk question
# 1  
Old 01-06-2015
Quick awk question

Code:
gawk 'BEGIN{count=0} /^Jan  5 04:33/,0 && /fail/ && /09x83377/ { count++ } END { print count }' /var/log/syslog

what is wrong with this code? i want to search the strings "fail" and "09x83377" from all entries. im grabbing all entries in the log starting from Jan 5 04:33 to the end of the file.
# 2  
Old 01-06-2015
Quote:
Originally Posted by SkySmart
Code:
gawk 'BEGIN{count=0} /^Jan  5 04:33/,0 && /fail/ && /09x83377/ { count++ } END { print count }' /var/log/syslog

what is wrong with this code? i want to search the strings "fail" and "09x83377" from all entries. im grabbing all entries in the log starting from Jan 5 04:33 to the end of the file.
Hello Skysmart,

Could you please let us know if these strings which you are searching for are in a single line or multiple lines(Which I think may bein single line only), also if you can provide us your os details with Input data it will be helpful for us to guide/advise.

EDIT: Seems you have added some information now Smilie, let's try the following command (Not tested though) for same and
let me know if this helps.
Code:
awk '/^Jan 5 04:33/{if($0 ~ /fail/ && $0 ~ /09x83377/){print $0}}' Input_file
OR
awk '/^Jan 5 04:33/{if($0 ~ /fail/ && $0 ~ /09x83377/){COUNT++}} END{print COUNT}' Input_file

NOTE: Make sure you are searching correct string Jan 5 04:33 with proper spaces in it.


Thanks,
R. Singh

Last edited by RavinderSingh13; 01-06-2015 at 06:20 AM.. Reason: Added solution as user added comments for input
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-06-2015
Hi SkySmart,
The standards describe a pattern (in an awk command) as:
Quote:
A pattern is any valid expression, a range specified by two expressions separated by a comma, or
one of the two special patterns BEGIN or END.
In the awk command:
Code:
/^Jan  5 04:33/,0 && /fail/ && /09x83377/ { count++ }

You have a range that is specified by the two expressions:
/^Jan 5 04:33/ and 0 && /fail/ && /09x83377/
The zero in the 2nd expression guarantees that that expression will evaluate to false for every line; so the range starts on the 1st line that matches the extended regular expression specified by the 1st expression and continues to the end of the input file(s). The standards don't provide any way to include both a range and another expression in the same pattern. (In other words, the meaning of:
Code:
( /^Jan  5 04:33/,0 ) && /fail/ && /09x83377/ { count++ }

is not defined to have any meaning in conforming awk utilities. Of course, gawk provides several extensions to the standards. But I don't know of any version of awk that supports this type of pattern as an extension to the standards.)

Hopefully, this explains what is wrong with your gawk script.

If what you want is to select and count the set of lines that contain both the string fail and the string 09x83377 on or after the 1st line that contains Jan 5 04:33 at the start of a line, Ravinder's suggestion should be enough for you to fix your problem. If that isn't what you want, please explain more clearly which lines you are trying to count. (And provide some sample input for us to use to test possible scripts.)
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 01-06-2015
Try (untested):
Code:
awk '/^Jan  5 04:33/ {C=1} C && /fail/ && /09x83377/ { count++ } END {print count}' file

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk :quick question removing empty line.

How to write in awk to remove lines starting with "#" and then process the file: This is not working: cat file|awk '{if ($0 ~ /^#/) $0="";print NF>0}' When I just give cat file|awk '{if ($0 ~ /^#/) $0="";print }' it prints the blank lines . I don't wnat the blank lines along with the... (15 Replies)
Discussion started by: rveri
15 Replies

2. Shell Programming and Scripting

awk Help: quick and easy question may be: How to use &&

Hi Guru's. I am trying to use to check if $5 is greater than 80 & if not 100, then to print $0 : awk '{ if ($5>80) && if ($5 != 100) print $0} But getting error: >bdf1|sed 's/%//g'|awk '{ if ($5>80) && if ($5 != 100) print $0}' syntax error The source line is 1. The error... (6 Replies)
Discussion started by: rveri
6 Replies

3. Shell Programming and Scripting

Quick sed/awk question

Hi fellow linux-ers, I have a quick question for you. I have the following text, which I would like to modify: 10 121E(121) 16 Jan 34S 132E 24 Feb 42 176E(176) 18 Sep 21S 164E 25 May 15 171W(-171) 09 Jul How can I do the following 2 modifications using sed and/or awk? 1. in 1st column,... (1 Reply)
Discussion started by: lucshi09
1 Replies

4. Shell Programming and Scripting

Quick question

When I have a file like this: 0084AF aj-123-a NAME Ajay NAME Kumar Engineer 015ED6 ck-345-c 020B25 ef-456-e 027458 pq-890-p NAME Peter NAME Salob Doctor 0318F0 xy-123-x NAME Xavier Arul NAME Yesu Supervisor 0344CA de-456-d where - The first NAME is followed by... (6 Replies)
Discussion started by: ajay41aj
6 Replies

5. Shell Programming and Scripting

quick question

I am using sed to find a pattern in a line and then I want to retain the pattern + the rest of the line. How is this possible? ie: line is: 14158 05-15-08 20:00 123-1234-A21/deliverable/dhm.a search for 123-1234-A21 ie: echo $line | sed 's/.*\(\{3\}-\{4\}-\{3\}\{5\}\).*/\1/' ... (1 Reply)
Discussion started by: phreezr
1 Replies

6. Shell Programming and Scripting

Help with AWK -- quick question

Ok. I'm just starting to use AWK and I have a question. Here's what I'm trying to do: uname -n returns the following on my box: ftsdt-svsi20.si.sandbox.com I want to pipe this to an AWK statement and make it only print: svsi20 I tried: uname -n | awk '{ FS = "." ; print $1 }' ... (5 Replies)
Discussion started by: Probos
5 Replies

7. UNIX for Dummies Questions & Answers

Quick question

Hi, Is there a simple way, using ksh, to find the byte position in a file that a stated character appears? Many thanks Helen (2 Replies)
Discussion started by: Bab00shka
2 Replies

8. UNIX for Dummies Questions & Answers

Quick Question

Hi, I am new to UNIX, and am learning from this tutorial : http://www.ee.surrey.ac.uk/Teaching/Unix/index.html It keeps telling me to files downloaded from the internet (like .txt files) to the directory, and I dont know how to. How do I add .txt files to my directory? Thanks. (6 Replies)
Discussion started by: IAMTHEEVILBEAN
6 Replies

9. UNIX for Dummies Questions & Answers

Quick Question

Hello There! I am trying to write this SIMPLE script in Bourne Shell but I keep on getting syntax errors. Can you see what I am doing wrong? I've done this before but I don't see the difference. I am simply trying to take the day of the week from our system and when the teachers sign on I want... (7 Replies)
Discussion started by: catbad
7 Replies

10. UNIX for Dummies Questions & Answers

A Quick Question

Wat is the difference between the cp mv ln etc in /usr/sbin/static and cp mv ln functions in /usr/bin (4 Replies)
Discussion started by: DPAI
4 Replies
Login or Register to Ask a Question