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?

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:

grep '^HOOK$' file_name

Once you have down the successful match the opposite will work in this case, using the -v switch.

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.

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?

# grep a file, return first result and stop
grep -m1 "HOOK" file.txt

# grep a file, only return result with HOOK on line by itself
grep -P "^HOOK$" file.txt
# you can also use egrep (or instead of -P use pgrep)

Aia - You beat me to it! Well, if the original poster wants to limit the number of matches, then -m1 would work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.