LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep a single word...not two! (https://www.linuxquestions.org/questions/linux-newbie-8/grep-a-single-word-not-two-4175467215/)

Drigo 06-24-2013 04:20 PM

grep a single word...not two!
 
So if I use:

$<my command> | grep dim4
#I get to options:

Output :
dim4 42
pixdim4 1.0000000000


But I would like just the 'dim4 ' line to appear without the pixdim4 line....like this:


$<my command> | grep dim4
#I want the output just with the first line:

Output:
dim4 42


How can i make it work?
Thanks in advance!

druuna 06-24-2013 04:29 PM

If dim is always at the beginning of the line try:
Code:

grep "^dim4"
And if it is not:
Code:

grep "\<dim4\>"

linuxCode 06-24-2013 04:37 PM

try this

<my command> | grep -w 'dim4'

eklavya 06-25-2013 12:36 AM

You can try this too.
Code:

<my command> | grep "\bdim4"

Madhu Desai 06-25-2013 12:58 AM

Quote:

Originally Posted by Drigo (Post 4977809)
But I would like just the 'dim4 ' line to appear without the pixdim4 line....like this:

#I want the output just with the first line:

If you just want to search for first line, then there is no need for grep.
Code:

$ <command> | head -n1
But if you are referring to return only first hit, then, any of these will work
Code:

$ <command> | grep -w -m1 'dim4'
$ <command> | grep -m1 '\<dim4\>'
$ <command> | grep -m1 '\bdim4\b'


shivaa 06-25-2013 01:09 AM

No need to make it complecated. Just use -w option to grep some specific regexp. From manual of grep:
Quote:

-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.

Code:

~$ <my_command> | grep -w 'dim4'

Drigo 06-26-2013 12:34 PM

Great!!! The fist two posts worked :)
This post and you guys are great!


All times are GMT -5. The time now is 08:56 PM.