Grep question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep question
# 1  
Old 03-01-2011
Grep question

All,
I am wanting to find out if I can do this in one grep statement
Code:
grep -R failed * |grep -iEw 'Mar 1|Feb 2'


I want to search all files in a directory for the text "failed" AND a "date or date".

Currently, I am using the above running one grep and then piping it to another. It works, but i was wondering if this could be done in one line.

Last edited by Franklin52; 03-01-2011 at 03:38 PM.. Reason: Please use code tags, thank you
# 2  
Old 03-01-2011
Code:
find . -type f -exec awk '/failed/&&(/Mar 1/||/Feb 2/) {print FILENAME ":" $0}' {} \;

# 3  
Old 03-01-2011
Hi.

I can't tell if you want the filenames or the specific lines. If you want the filenames, should the matches be all on the same line or could they be anywhere in the file? Here is a use of non-standard utility glark that shows all 3 of those possibilities:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate combination of string matching, glark.
# glark-home http://www.incava.org/projects/glark/

# Section 1, setup, pre-solution.
# Infrastructure details, environment, commands for forum posts. 
# Uncomment export command to test script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
C=$HOME/bin/context && [ -f $C ] && . $C specimen glark
set -o nounset
pe

# Section 2, display input file.
# Display tree and sample of data files.
pe " Tree view of sample-data directory:"
tree -a sample-data
pe
pe " || start [ first:middle:last ]"
specimen -n sample-data/data* || pe " The code specimen is missing, skipped."
pe " || end"

# Section 3, solution.

pl " Results, filenames with matches anywhere in file:"
glark --files-with-matches -d recurse --and=-1 failed \
--or "Mar 1" "Feb 2" --end-of-or --end-of-and sample-data

pl " Results, filenames with matches all on same line:"
glark --files-with-matches -d recurse --and=0 failed \
--or "Mar 1" "Feb 2" --end-of-or --end-of-and sample-data

pl " Results, lines numbered, matches all on same line:"
glark --no-highlight -d recurse --and=0 failed \
--or "Mar 1" "Feb 2" --end-of-or --end-of-and sample-data

exit 0

on my system, producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.7 (lenny) 
GNU bash 3.2.39
specimen (local) 1.17
glark, version 1.8.0

 Tree view of sample-data directory:
sample-data
|-- data1
|-- data2
`-- data3

0 directories, 3 files

 || start [ first:middle:last ]
Whole: 5:0:5 of 7 lines in file "sample-data/data1"
     1	First stuff in file
     2	...
     3	succeed
     4	...
     5	Mar 1
     6	...
     7	Last stuff in file

Whole: 5:0:5 of 7 lines in file "sample-data/data2"
     1	First stuff in file
     2	...
     3	failed
     4	...
     5	Mar 1
     6	...
     7	Last stuff in file

Whole: 5:0:5 of 5 lines in file "sample-data/data3"
     1	First stuff in file
     2	...
     3	failed, but has a date on the same line: Mar 1
     4	...
     5	Last stuff in file
 || end

-----
 Results, filenames with matches anywhere in file:
sample-data/data2
sample-data/data3

-----
 Results, filenames with matches all on same line:
sample-data/data3

-----
 Results, lines numbered, matches all on same line:
sample-data/data3
    3 failed, but has a date on the same line: Mar 1

The glark command is not fast, but it has a lot of features for searching. If you are interested, you'll need ruby; see the URL noted in the script.

Best wishes ... cheers, drl
# 4  
Old 03-02-2011
I wanted the filenames and the results, but since my command works i'm not going to worry about it
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Grep Question

My grep returns a row of data like this: 75=20130130;60=074338;61=985;511=55473883;452=115439;62=196;267=1; Is there a way for the grep to only return 60="something" and 511="something" ? Thanks in advance. (10 Replies)
Discussion started by: Carl2013
10 Replies

2. UNIX for Dummies Questions & Answers

Question on grep

Hello all, I'm trying to grep the string "scott" from all files whose names are like srvr*.log and that were created "Nov 15"...I'm trying the following command but throws an error message...seems like the syntax is incorrect.. grep scott < ls -l srvr*.log|grep "Nov 15" Thanks for your... (9 Replies)
Discussion started by: luft
9 Replies

3. Shell Programming and Scripting

Question about grep

is there anyway i can ask grep to only get the first line? as in the top command line line 1 <-- just grep this line line 2 line 3 ---------- Post updated at 04:24 PM ---------- Previous update was at 04:19 PM ---------- nvm.. found out that i can do it with |head (12 Replies)
Discussion started by: Nick1097
12 Replies

4. Shell Programming and Scripting

Question about grep

can anyone tell me what the \/$ means? from grep \/$ (8 Replies)
Discussion started by: Nick1097
8 Replies

5. Shell Programming and Scripting

grep question please

i have files with "DOMAINSOLVER ACMS" with any number of spaces in between the two words on its own line and i can find it with the following: grep -c "DOMAINSOLVER* ACMS" $FILENAMEbut i need to exclude any lines matching: "$DOMAINSOLVER". i've tried a variety of quoting and escaping with no luck.... (4 Replies)
Discussion started by: crimso
4 Replies

6. Shell Programming and Scripting

grep question

Hello, Is there a way in grep to remember patterns? For eg: int a,b,c,d,a; If a variable is declared twice, like in the previous example, I should be able to print only those lines. Is there a way to print only the lines where the variable name occurs more than once, using grep... (1 Reply)
Discussion started by: prasanna1157
1 Replies

7. UNIX for Dummies Questions & Answers

grep question

Instead of using the following command #dmesg | grep -v sendmail | grep -v xntpd How can I use just one grep -v and give both arguments. Please suggest thanks (4 Replies)
Discussion started by: Tirmazi
4 Replies

8. Shell Programming and Scripting

grep question

hello people, All my servers have 4 mounts with this norme. For example, if my hostname is siroe. df -h | grep `hostname` /dev/dsk/c1t3d0s6 404G 399G 800M 100% /siroe3 /dev/dsk/c1t2d0s6 404G 399G 800M 100% /siroe2 /dev/md/dsk/d6 20G 812M 19G ... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies

9. UNIX for Dummies Questions & Answers

Grep Question

Hello Everybody, I have files; yyyymmdd.log which the data look like this; "Txid=9426043&MsgTxt=Thankyou&UserId=john&Password=jh2501" "Txid=9426150&MsgTxt=Thankyou&UserId=john&Password=jh2501" . . . "Txid=9426200&MsgTxt=Thankyou&UserId=john&Password=jh2501" Question 1: How to... (3 Replies)
Discussion started by: nazri76
3 Replies

10. UNIX for Dummies Questions & Answers

grep question

what is the format for grep if I want to search from the current directory and through all its subdirectories?:) (3 Replies)
Discussion started by: pkappaz
3 Replies
Login or Register to Ask a Question