LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-24-2013, 05:20 PM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
Angry 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!
 
Old 06-24-2013, 05:29 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
If dim is always at the beginning of the line try:
Code:
grep "^dim4"
And if it is not:
Code:
grep "\<dim4\>"

Last edited by druuna; 06-24-2013 at 05:32 PM.
 
Old 06-24-2013, 05:37 PM   #3
linuxCode
Member
 
Registered: Jun 2013
Location: New York City
Distribution: Arch
Posts: 33

Rep: Reputation: 43
try this

<my command> | grep -w 'dim4'
 
Old 06-25-2013, 01:36 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
You can try this too.
Code:
<my command> | grep "\bdim4"
 
Old 06-25-2013, 01:58 AM   #5
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by Drigo View Post
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'

Last edited by mddnix; 06-25-2013 at 02:01 AM.
 
Old 06-25-2013, 02:09 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
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'
 
Old 06-26-2013, 01:34 PM   #7
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Great!!! The fist two posts worked
This post and you guys are great!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep a word from a file thomas2004ch Linux - Software 11 03-28-2013 12:49 PM
[SOLVED] How to grep for an exact word? Arpitha10 Linux - Newbie 8 12-16-2012 07:23 AM
grep a word from some files??? shahz Red Hat 3 09-06-2008 04:48 AM
Word count with grep DiagonalArg Linux - Software 3 02-13-2006 01:46 PM
grep to NOT select a word robertmarkbram Programming 2 08-21-2003 11:48 PM


All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration