The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can I read a file character by character? murtaza Shell Programming and Scripting 4 04-27-2009 06:51 AM
Can i read a file character by character karnan Shell Programming and Scripting 6 05-19-2008 03:22 AM
Bourne: search for a non-numeric character in $VAR lumix Shell Programming and Scripting 2 12-14-2007 02:13 PM
Search in variable for character rorey_breaker Shell Programming and Scripting 1 09-27-2007 08:05 AM
get last character in a file sunil_neha Shell Programming and Scripting 1 08-12-2004 03:52 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-05-2006
misenkiser misenkiser is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Search for a non zero character in file

Hi,

I have a log file which has entries as

Staged 0 records from fn.dat (0 failed)
01/01 01:01:01 I 0 Error Transactions


I want to find out any line that has an entry like "(1 failed)"
or "(2 failed)" or any number in general ( >0 )

similarly it should search for string like "1 Error" or "2 Error"
ie anything greater than 0

Thanx in advance
  #2 (permalink)  
Old 10-05-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798

Code:
sed -n -e "/[1-9][0-9]* failed/p" -e "/[1-9][0-9]* Error/p" log.file

  #3 (permalink)  
Old 10-05-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,557
Alternative in Python, assuming (...) is always at the end


Code:
for lines in open("input.dat"):
        lines = lines.strip()
        if lines.find("(") != -1:
                print lines

  #4 (permalink)  
Old 10-07-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 579
Using awk:
Code:
awk '$0 ~ /[1-9][0-9]* [failed|Error]/ {print}' log.file

Using sed:
Code:
sed -n '/[1-9][0-9]* [failed|Error]/p' log.file


Last edited by tayyabq8; 10-07-2006 at 04:30 AM..
  #5 (permalink)  
Old 10-09-2006
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 439

Code:
[failed|Error]

This is not going to do what you expect. It will match *any single* character in that regex character class. For example:

Code:
echo "Staged 10 records from fn.dat (2 accepted)" | sed -n '/[1-9][0-9]* [failed|Error]/p'

will match because we have a numeric followed by space and the letter "a" which is in the character class. Not only that, it will also match 10 records

To understand the logic of the regex do this:

Code:
echo "Staged 10 records from fn.dat (2 accepted)" | grep --color '[1-9][0-9]* [failed|Error]'

If your grep's GREP_COLOR environment variable is configured you should get this:
---> $ Staged 10 records from fn.dat (2 accepted)

To cut a long story short, the correct solution could be any of the following:

Code:
awk '$0 ~ /[1-9][0-9]* (failed|Error)/ {print}' file
sed -n '/[1-9][0-9]* \(failed\|Error\)/p' file  # yucky !
sed -nr '/[1-9][0-9]* (failed|Error)/p' file    # more readable with option -r
grep '[1-9][0-9]* \(failed\|Error\)' file       # yucky again!
grep -E '[1-9][0-9]* (failed|Error)' file

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0