Unix/Linux Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

using sed or grep to find exact match of text

UNIX for Dummies Questions & Answers


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-22-2008
risk_sly risk_sly is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 17 July 2009, 9:47 PM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
using sed or grep to find exact match of text

Hi,

Can anyone help me with the text editing I need here. I have a file that contains the following lines for example: (line numbers are for illustration only)

1 Hello world fantasy.
2 Hello worldfuntastic.
3 Hello world wonderful.

I would like to get all those lines of text that contains the word "world" without anything after it. Meaning, I would like to have lines 1 and 3, line 2 should not be included because it contains "worldfuntastic", no space between.

1 Hello world fantasy.
3 Hello world wonderful

The problem is, there are some lines that use TAB to separate words like line 3. When I use grep "world " <filename>, this will get all lines of text with the word "world" and space after it but not those with TAB, i.e. the results would only be:
1 Hello world fantasy

line 3 is not included because "world" and "wonderful" are separated by TAB. Any suggestions, please?
Sponsored Links
    #2  
Old Unix and Linux 10-22-2008
zaxxon's Unix or Linux Image
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 27 August 2015, 8:09 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,313
Thanks: 135
Thanked 469 Times in 423 Posts

Code:
egrep "[[:space:]]world[[:space:]]" infile

The [[:space:]] works with any kind and number of concatenated spaces, wether it's blanks or tabs etc.
Sponsored Links
    #3  
Old Unix and Linux 10-22-2008
sivakumar.rj sivakumar.rj is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 7 August 2009, 6:26 AM EDT
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
Error

I tried with egrep [[:space:]]....but i think it doesn't work:

I just tried with this grep " world " infile....(include the spaces between the before and after the word "world".
    #4  
Old Unix and Linux 10-22-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
Quote:
Originally Posted by risk_sly View Post
Hi,

Can anyone help me with the text editing I need here. I have a file that contains the following lines for example: (line numbers are for illustration only)

1 Hello world fantasy.
2 Hello worldfuntastic.
3 Hello world wonderful.

I would like to get all those lines of text that contains the word "world" without anything after it. Meaning, I would like to have lines 1 and 3, line 2 should not be included because it contains "worldfuntastic", no space between.

1 Hello world fantasy.
3 Hello world wonderful

The problem is, there are some lines that use TAB to separate words like line 3. When I use grep "world " <filename>, this will get all lines of text with the word "world" and space after it but not those with TAB, i.e. the results would only be:
1 Hello world fantasy

line 3 is not included because "world" and "wonderful" are separated by TAB. Any suggestions, please?

if you want to grab whole words, the basic logic is to go through each word, and check against what you want to find using equality "==" operator

Code:
awk '{
 for ( i=1;i<=NF;i++){
  if ( $(i) == "world"){
   print $0
  } 
 }
}
' file

Sponsored Links
    #5  
Old Unix and Linux 10-22-2008
Tytalus's Unix or Linux Image
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 19 May 2015, 6:39 AM EDT
Location: Scotland
Posts: 594
Thanks: 1
Thanked 28 Times in 27 Posts
what about grep -w :


Code:
     -w       Searches for the expression as a word  as  if  surrounded by \< and \>.


Code:
#  grep -w world infile
1 Hello world fantasy.
3 Hello world   wonderful.

Sponsored Links
    #6  
Old Unix and Linux 10-22-2008
risk_sly risk_sly is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 17 July 2009, 9:47 PM EDT
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
thanks for the reply guys.
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep and sed exact match questions thibodc Shell Programming and Scripting 2 05-24-2012 02:37 AM
grep and sed exact match questions thibodc UNIX for Dummies Questions & Answers 1 05-24-2012 02:35 AM
QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed thibodc Shell Programming and Scripting 1 05-23-2012 11:14 PM
grep exact match rob171171 Solaris 4 03-25-2011 07:59 AM
Exact Match thru grep ????? manas_ranjan UNIX for Advanced & Expert Users 2 08-17-2007 05:57 AM



All times are GMT -4. The time now is 01:08 AM.