using sed or grep to find exact match of text | Unix Linux Forums | UNIX for Dummies Questions & Answers

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 10-22-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 28 August 2014, 10:17 AM EDT
Location: St. Gallen, Switzerland
Posts: 6,227
Thanks: 121
Thanked 451 Times in 411 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 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 10-22-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 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 10-22-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 10 January 2014, 5:37 AM EST
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 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 05:08 PM.