![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 |
|
||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|