grep two strings in a file.. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

grep two strings in a file..

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-22-2010
intiraju intiraju is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 24 February 2010, 4:28 PM EST
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
grep two strings in a file..

Hello All,
I have a big file about 1000 lines. Now i am trying to grep a particular string and printing the lines from the string.

say for example in 500th line i have the date as "Mon Wed 14 20:15:24 2010". now i in my case i need to grep the combination of the strings "Mon Wed 14" and the other string "2010". i need to ignore the time among the whole string. so in one command i need to grep the two strings and ignore the time alone.

Please help in this regard. Thanks in advance.

Raju
Sponsored Links
    #2  
Old 02-23-2010
devtakh devtakh is offline Forum Advisor  
Unix Enthusiatic
 
Join Date: Oct 2007
Last Activity: 21 August 2013, 5:20 AM EDT
Location: Bangalore
Posts: 738
Thanks: 0
Thanked 7 Times in 7 Posts

Code:
sed -n -e '/Mon Wed 14 [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\} 2010/p' file1

cheers,
Devaraj Takhellambam
Sponsored Links
    #3  
Old 02-23-2010
thillai_selvan's Avatar
thillai_selvan thillai_selvan is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 5 July 2013, 6:10 AM EDT
Location: Chennai
Posts: 190
Thanks: 0
Thanked 1 Time in 1 Post
You can use the grep command as follows


Code:
grep "Mon Wed 14.*2010" filename

So this will match the combination of the strings Mon Wed 15 and 2010 from that lines.

Last edited by Scott; 02-23-2010 at 07:16 AM.. Reason: Added code tags
    #4  
Old 02-23-2010
dennis.jacob's Avatar
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 30 March 2014, 9:33 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
Try:


Code:
grep 'Mon Wed 14 [0-2][0-9]:[0-5][0-9]:[0-5][0-9] 2010' file

Sponsored Links
    #5  
Old 02-23-2010
amitranjansahu's Avatar
amitranjansahu amitranjansahu is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 6 February 2014, 9:06 AM EST
Location: Bangalore,INDIA
Posts: 507
Thanks: 4
Thanked 27 Times in 27 Posts
try

cat file | grep "Mon Wed 14 20" | grep "2010"
Sponsored Links
    #6  
Old 02-23-2010
dennis.jacob's Avatar
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 30 March 2014, 9:33 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
Quote:
Originally Posted by amitranjansahu View Post
try

cat file | grep "Mon Wed 14 20" | grep "2010"
Few points:

1. Useless use of cat. No need of cat there
2. It fail for conditions such as
Mon Wed 14 20 12:10:10 2009 djflsdjfjdsjjf2010
Sponsored Links
    #7  
Old 02-23-2010
vivekraj's Avatar
vivekraj vivekraj is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 9 July 2010, 8:42 AM EDT
Location: Chennai
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Date Matching:

You can use the following command,which will work for any kind of date.


Code:
egrep "[A-Z][a-z]{2} [A-Z][a-z]{2} [0-9]{2} [0-2][0-9]:[0-5][0-9]:[0-5][0-9] [0-9]{0,4}" file


Last edited by Scott; 02-23-2010 at 07:15 AM.. Reason: Please use code tags
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep a string in the lines between 2 strings of a file bhagirathi Shell Programming and Scripting 8 07-10-2009 04:47 AM
Grep Multiple Strings durgaprasad Shell Programming and Scripting 10 06-26-2009 06:38 AM
grep strings of a certain length angela.perez7 Shell Programming and Scripting 2 02-14-2009 02:39 PM
Grep strings from file and put in Column thepurple Shell Programming and Scripting 2 12-08-2008 02:50 AM
want to grep only strings in a file? balan_mca Shell Programming and Scripting 5 11-03-2008 05:19 AM



All times are GMT -4. The time now is 12:31 AM.