Help with grep?

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 43
Reputation: Mattpd is an unknown quantity at this point 
Solved Threads: 0
Mattpd Mattpd is offline Offline
Light Poster

Help with grep?

 
0
  #1
Nov 11th, 2009
If I have two lines of text:

HOOK
HOOK HOOK

what is the syntax to grep just the first line without the second? and the second without the first?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven
 
0
  #2
Nov 11th, 2009
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic
 
3
  #3
Nov 11th, 2009
Originally Posted by Mattpd View Post
If I have two lines of text:

HOOK
HOOK HOOK

what is the syntax to grep just the first line without the second? and the second without the first?
Find any difference between both and then match it. e.g. with HOOK by itself in a line we can match the beginning and end of the line. Thus:
Shell Scripting Syntax (Toggle Plain Text)
  1. grep '^HOOK$' file_name
Once you have down the successful match the opposite will work in this case, using the -v switch.
Shell Scripting Syntax (Toggle Plain Text)
  1. grep -v '^HOOK$' file_name
Will match HOOK HOOK
However, if there were another line containing something that is not ^HOOK$ it will produce a match as well.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Xydric is an unknown quantity at this point 
Solved Threads: 3
Xydric Xydric is offline Offline
Newbie Poster
 
0
  #4
Nov 11th, 2009
Are you wanting to try and grep "HOOK" on a line by itself, or specifically just the first match?

There are all sorts of things that you could actually want to know, although I am not sure which you are wanting. What exactly is it that you are needing to do?

Shell Scripting Syntax (Toggle Plain Text)
  1. # grep a file, return first result and stop
  2. grep -m1 "HOOK" file.txt
  3.  
  4. # grep a file, only return result with HOOK on line by itself
  5. grep -P "^HOOK$" file.txt
  6. # you can also use egrep (or instead of -P use pgrep)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: Xydric is an unknown quantity at this point 
Solved Threads: 3
Xydric Xydric is offline Offline
Newbie Poster
 
0
  #5
Nov 11th, 2009
Aia - You beat me to it! Well, if the original poster wants to limit the number of matches, then -m1 would work
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Shell Scripting Forum


Views: 679 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC