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
xargs -P otheus UNIX for Advanced & Expert Users 4 02-06-2009 04:14 PM
search ")" with egrep - egrep: syntax error sagarjani UNIX for Dummies Questions & Answers 7 10-14-2008 08:30 AM
Egrep cheat sheet anywhere? Looking for meaning of egrep -c leelm UNIX for Dummies Questions & Answers 2 01-11-2008 03:37 PM
Help with xargs JimJim UNIX for Dummies Questions & Answers 4 02-08-2005 07:08 PM
xargs jpprial UNIX for Dummies Questions & Answers 4 09-17-2001 09:29 PM

Reply
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 07-23-2009
johnbach johnbach is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
Angry xargs egrep

Below is a sample script


Code:
jb>cat search
#!/bin/bash
set -x
PATTERN='"'`awk -F: '{printf $2 "|"} END {printf "\b"}' RULE`'"'
echo 'Pattern ['$PATTERN']'
ls -tr *$1* |xargs egrep "$PATTERN"

Sample RULE file

Code:
jb>cat RULE
P1 :pone
P2 :ptwo

Sample Output

Code:
jb>./search f
++ awk -F: '{printf $2 "|"} END {printf "\b"}' RULE
+ PATTERN='"pone|ptwo"'
+ echo 'Pattern ["pone|ptwo"]'
Pattern ["pone|ptwo"]
+ ls -tr f1 f1.txt f2 f3 f4
+ xargs egrep '"pone|ptwo"'
f2:ptwo
jb>

Directly searching


Code:
jb>ls -tr *f* |xargs egrep "pone|ptwo"
f1:000pone000
f2:ptwo

Sample file content

Code:
jb>cat f1
11
000pone000
1111
jb>cat f2
222
ptwo
22222222
jb>





Why 'search' doesn't list f1?
  #2 (permalink)  
Old 07-23-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Try the pattern without the double quotes, replace this line:


Code:
PATTERN='"'`awk -F: '{printf $2 "|"} END {printf "\b"}' RULE`'"'

with:


Code:
PATTERN=`awk -F: '{printf $2 "|"} END {printf "\b"}' RULE`

  #3 (permalink)  
Old 07-23-2009
johnbach johnbach is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
Thanks,it works.
I have one more question.

I have some 20 patterns OR'ed 'pat1|pat2|.....pat20'
Each pattern of length around 20 chars
and i search around 30 files each of size 10 MB.
It takes 2 mins to complete !
Is there any optimal/fast way to do this ?
  #4 (permalink)  
Old 07-23-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
You can try something like:


Code:
awk -F: '{print $2}' RULE > tmp.file

grep -f tmp.file $(ls -tr *$1*)

  #5 (permalink)  
Old 07-23-2009
johnbach johnbach is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
1.Actually the RULE file names pattern,(RULE file as such is not a pattern) example

Code:
>cat RULE
SUCCESS       :Message sent successfully to
FAILURE         :Message send failed for
Acknowledged :Got ack from  
#Goes on

2.This line extract the second column ,forms a (ORed) pattern


Code:
PATTERN=`awk -F: '{printf $2 "|"} END {printf "\b"}' RULE`

#PATTERN will be  'Message sent successfully to|Message send failed for|Got ack from'

3.Search for all pattern in all file and redirect the o/p to tmp.txt (Takes 2 mins )

Code:
ls -tr *$1* |xargs egrep "$PATTERN" > tmp.txt

4.from tmp.txt do some more filtering,collect statistics (count each pattern)
(Dont have problem with this step ,takes very less time.)


Code:
Sample Output :
SUCCESS :1423
FAILURE : 432
Acknowledged : 764


But step 3 alone takes much of the time(around 2 mins)
  #6 (permalink)  
Old 07-23-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,348
Have you tried my last 2 commands for steps 2 and 3?


Code:
awk -F: '{print $2}' RULE > tmp.file

grep -f tmp.file $(ls -tr *$1*) > tmp.txt

  #7 (permalink)  
Old 07-23-2009
johnbach johnbach is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 54
Once again thanks,It works
Search completed in less than 30 secs.

My grep version doesn't support -f ,fgrep does.

Code:
awk -F: '{print $2}' RULE > tmp.txt
fgrep -f tmp.txt $(ls -tr *26_06_2009*) > tmp2.txt

(Sorry I thought you didn't get my question,but actually I misunderstood your solution )
Reply

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 04:01 AM.


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