Search for a non zero character in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for a non zero character in file
# 1  
Old 10-05-2006
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  
Old 10-05-2006
Code:
sed -n -e "/[1-9][0-9]* failed/p" -e "/[1-9][0-9]* Error/p" log.file

# 3  
Old 10-05-2006
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  
Old 10-07-2006
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  
Old 10-09-2006
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

2. UNIX for Dummies Questions & Answers

Search for a particular word and replace the first character

Hi Unix gurus, I've a dna sequence in a file format known as fasta format (sequence header starts with > and ignored), an example shown below: >sequence_1 CGTATTCTCCGAATACC ATACG >sequence_2 CAGATTTTCAAATACCCCC In a file like this I want to do the following three search and replace. The... (4 Replies)
Discussion started by: Fahmida
4 Replies

3. UNIX for Dummies Questions & Answers

How to search for a file having a particular character in a particular place in a directory.?

Hi Guys, I want to search for a specific file in a directory which have a "b" letter as the 3rd character in the name of the file. For Example : /abc/efg/ldbjfblkj.sh /abc/efg/erublkd.sh /abc/efg/eibueora.sh /abc/efg/kfvnmnb.sh Since we have 2 files with "b" as a 3rd character in... (5 Replies)
Discussion started by: Pramod_009
5 Replies

4. Shell Programming and Scripting

Ls help: ls not working with start character search (^)

Experts, How to list a file using ^ character, for all files started with character a. (os= hp-ux ) # ls -l -rw------- 1 useradm users 0 Mar 26 14:30 abc -rw------- 1 useradm users 0 Mar 26 14:30 def -rw------- 1 useradm users 0 Mar 26... (7 Replies)
Discussion started by: rveri
7 Replies

5. Shell Programming and Scripting

bash script search file and insert character when match found

Hi I need a bash script that can search through a text file and when it finds 'FSS1206' I need to put a Letter F 100 spaces after the second instance of FSS1206 The format is the same throughout the file I need to repeat this on every time it finds the second 'FSS1206' in the file I have... (0 Replies)
Discussion started by: firefox2k2
0 Replies

6. Shell Programming and Scripting

awk search for space character

How do I use awk to search for a string that contains a space bar? I have tried this awk '/send email/ {print $0}' input awk '/send\ email/ {print $0}' input (1 Reply)
Discussion started by: locoroco
1 Replies

7. Shell Programming and Scripting

Search Character

Hi, I need to search the "BP1418" in xml file(the letter BP remain constant but the number changes randomly and it will be in double quotes) . Can you please let me know how to find these charater in unix. thanks (3 Replies)
Discussion started by: ravi214u
3 Replies

8. UNIX for Dummies Questions & Answers

how to search and list file with wildcard character

hi, I want to search all files in the current working direcotry and to print in comma (,) seperated output. But I have two patterns to search for. Files will be in ABC20100508.DAT format. Search should happen on the format (ABC????????.DAT) along with date(20100508). I can do a ls... (2 Replies)
Discussion started by: anandapani
2 Replies

9. UNIX for Dummies Questions & Answers

to search for a particular character in a word

Hi I would like to accept in a string from user like username/pwd@dbname suppose the user does not input @ then i should throw an error that @ symbol missing . How to achieve this Thanks in advance Suresh (6 Replies)
Discussion started by: ssuresh1999
6 Replies

10. Shell Programming and Scripting

Search in variable for character

qwertyuioplkjhgfdsa (1 Reply)
Discussion started by: rorey_breaker
1 Replies
Login or Register to Ask a Question