|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
Quote:
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
|
||||
|
||||
|
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
|
|||
|
|||
|
thanks for the reply guys.
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|