944,155 Members | Top Members by Rank

Ad:
Nov 11th, 2009
0

Help with grep?

Expand 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Mattpd is offline Offline
47 posts
since Nov 2009
Nov 11th, 2009
0
Re: Help with grep?
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 11th, 2009
3
Re: Help with grep?
Click to Expand / Collapse  Quote originally posted by Mattpd ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Nov 11th, 2009
0
Re: Help with grep?
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)
Reputation Points: 10
Solved Threads: 4
Newbie Poster
Xydric is offline Offline
10 posts
since Oct 2009
Nov 11th, 2009
0
Re: Help with grep?
Aia - You beat me to it! Well, if the original poster wants to limit the number of matches, then -m1 would work
Reputation Points: 10
Solved Threads: 4
Newbie Poster
Xydric is offline Offline
10 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: OMG... simple question driving me nuts
Next Thread in Shell Scripting Forum Timeline: korn script issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC