LinuxQuestions.org
Review your favorite Linux distribution.
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 02-01-2012, 06:16 AM   #1
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 537

Rep: Reputation: 32
How to grep a string with fixed length?


Hi all,

I want to grep a string such like MYHOME and use the command
Code:
grep MYHOME *
But I got a lot of results such as _MYHOME, MYHOME_ etc. What I want is just show the line which contains MYHOME.
 
Old 02-01-2012, 06:47 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
Did you try reading the manual.
Here's a hint: -w
 
1 members found this post helpful.
Old 02-01-2012, 07:00 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
If the line contains only MYHOME then grep '^MYHOME$' *
 
Old 02-01-2012, 07:19 AM   #4
thomas2004ch
Member
 
Registered: Aug 2009
Posts: 537

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by fukawi1 View Post
Did you try reading the manual.
Here's a hint: -w
This is what I am looking for.

Many thanks.
 
Old 02-01-2012, 07:21 AM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
This is not simple task without knowing the context

eg if the word is surrounded with spaces, just:
Code:
grep ' MYHOME ' *
would do it

(edited my comment about -w, if it does it, fine )

Last edited by Cedrik; 02-01-2012 at 07:23 AM.
 
Old 02-01-2012, 07:24 AM   #6
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
Ill take your word for it Cedrik, i tested with the following,

Code:
=~ $ echo -e "abc 123\n_abc 456\nabc_ 789" | grep -w "abc"
abc 123
 
Old 02-01-2012, 07:30 AM   #7
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
Quote:
Originally Posted by fukawi1 View Post
Ill take your word for it Cedrik, i tested with the following,

Code:
=~ $ echo -e "abc 123\n_abc 456\nabc_ 789" | grep -w "abc"
abc 123
Yes, I edited my post as from the grep man:
Quote:
Word-constituent characters are letters, digits, and the underscore.
eg,
Code:
echo -e "-abc\n_abc 456\nabc_ 789" | grep -w "abc"
-abc

Last edited by Cedrik; 02-01-2012 at 07:32 AM.
 
  


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
How can I convert to fixed length format. rainmk Linux - Newbie 6 06-09-2011 05:47 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 02:34 PM
Get string length in C ++ for a c string without trailing \0 nc3b Programming 10 12-28-2007 10:46 AM
gawk, comma delim to fixed length? johniccp Programming 1 01-20-2005 05:21 PM
csv to fixed-length file roballen Programming 0 03-11-2004 04:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:15 AM.

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