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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep problem AshAdmin UNIX Desktop for Dummies Questions & Answers 1 01-31-2008 04:20 AM
Problem using grep Prem1201 Shell Programming and Scripting 3 01-09-2008 11:45 AM
Grep problem felixwhoals UNIX for Dummies Questions & Answers 1 12-17-2007 05:00 AM
grep problem yogesh_powar Shell Programming and Scripting 2 11-09-2006 03:43 PM
grep problem rraajjiibb Shell Programming and Scripting 4 11-17-2004 03:41 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-22-2008
Registered User
 

Join Date: Nov 2007
Posts: 55
problem using grep

hi all

i am using

grep -i abc 1_abc_unix
grep -i def 2_def_oracle

wirh this i will get the exact result.
but if the pattern contains the search patern then it is creating problem
like

grep -i abc 1_abc_unic
grep -i abc 1_def_abcd

so it will return trur for 1_def_abcd pattern also, but i dont want this , the result should only if the pattern matches within _ and _ .

using cut command and then searching would be cumbersome.
i hope there would be some short way out.

please help.
Reply With Quote
Forum Sponsor
  #2  
Old 07-22-2008
Registered User
 

Join Date: Dec 2007
Posts: 9
i think this will help
grep -i "\_abc\_" filename
Reply With Quote
  #3  
Old 07-22-2008
Registered User
 

Join Date: Nov 2007
Posts: 55
Quote:
Originally Posted by pooga17 View Post
i think this will help
grep -i "\_abc\_" filename

if the file is like:-
1_def_alpha_abc_bts then it will not work.

i want only first 2 _ to besearched.
Reply With Quote
  #4  
Old 07-22-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
I assume you know that the second argument to grep is a file name, and that you really mean

Code:
echo 1_abc_fnord | grep -wi abc
Perhaps grep -w would be sufficient. But if you only want matches on the second field, perhaps you should consider awk instead.

Code:
echo 1_abc_fnord | awk -F _ 'tolower($2) ~ /abc/'
If your awk doesn't have tolower(), see if you have nawk or mawk or gawk or XPG4 awk on your system somewhere.

Last edited by era; 07-22-2008 at 11:33 PM. Reason: awk syntax error, oops
Reply With Quote
  #5  
Old 07-22-2008
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 288
Quote:
Originally Posted by infyanurag View Post
if the file is like:-
1_def_alpha_abc_bts then it will not work.

i want only first 2 _ to besearched.
Check the grep -w option.

w option is for the exact search.

Thanks...
Reply With Quote
  #6  
Old 07-23-2008
Registered User
 

Join Date: Nov 2007
Posts: 55
Angry

Quote:
Originally Posted by namishtiwari View Post
Check the grep -w option.

w option is for the exact search.

Thanks...
grep -w did not work
Reply With Quote
  #7  
Old 07-23-2008
Registered User
 

Join Date: Nov 2007
Posts: 55
Quote:
Originally Posted by namishtiwari View Post
Check the grep -w option.

w option is for the exact search.

Thanks...


grep -w did not work.
please suggest something diff.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0