sed Exact Match when Dot is present


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed Exact Match when Dot is present
# 1  
Old 03-18-2015
Linux sed Exact Match when Dot is present

I am trying to replace exact word from my text. I know using the angled brackets does the trick. But it is not working when there is a dot in the text.

Code:
echo "Bottle BottleWater Bottle Can" | sed 's/\<Bottle\>//g'
 BottleWater  Can

But if my data has a dot or hash in it, it replaces all the occurrences of Bottle.
Code:
echo "Bottle Bottle.Water Bottle Can" | sed 's/\<Bottle\>//g'
 .Water  Can

Code:
echo "Bottle Bottle#Water Bottle Can" | sed 's/\<Bottle\>//g'
 #Water  Can

As you can see it is replacing all occurances of "Bottle" when there is a dot present.
The input data is not in my control. So I cant change or escape the characters.
Is there a way I can replace the exact match using sed, when a dot is present in the data?
I have to use Sed, since my code is generating a lot of sed text using -e options so I can run all of that in one go.

Any help would be much appreciated.

Thanks.
# 2  
Old 03-19-2015
Lose the g -

Code:
 echo "Bottle Bottle#Water Bottle Can" | sed 's/\<Bottle\>//'
 Bottle#Water Bottle Can

This will replace the FIRST occurrence of the word. One replacement only per line of text.
# 3  
Old 03-19-2015
Right, I am aware of that Jim. But I want only the exact words to be replaced. Not all.
It should not remove the Bottle from "Bottle.Water".
Is there a way to tell sed to treat dots like underscores while searching?
# 4  
Old 03-19-2015
Some versions of sed have options to use EREs instead of BREs which could shorten this considerably. Since you haven't told us what OS you're using, I'll give you the long, portable form (assuming your word delimiters are start of line, end of line, and space characters):
Code:
printf '%s\n' "$string" | sed -e 's/^Bottle / /' -e 's/ Bottle$/ /' -e 's/^Bottle$//' -e 's/ Bottle /  /g'


Last edited by Don Cragun; 03-19-2015 at 11:43 AM.. Reason: Change: -e '/s/^Bottle$//' to -e 's/^Bottle$//'
# 5  
Old 03-19-2015
Or try awk:
Code:
awk -v s="Bottle" '{for(i=1; i<=NF; i++) if($i==s) $i=""}1'

---
Quote:
Originally Posted by grep_me
I am trying to replace exact word from my text. I know using the angled brackets does the trick. But it is not working when there is a dot in the text.
The escaped angle brackets (\< and \>) indicate a (left/right) word boundary in some versions of sed (alphanumeric characters plus _ (underscore)).
Since a dot is not a word character, "Bottle" will get replaced..

Last edited by Scrutinizer; 03-19-2015 at 02:11 AM..
# 6  
Old 03-19-2015
Quote:
Originally Posted by Scrutinizer
Or try awk:
Code:
awk -v s="Bottle" '{for(i=1; i<=NF; i++) if($i==s) $i=""}1'

... ... ...
Note, however, adjacent spaces in the input will be coalesced to a single space by the above command. For instance.
Code:
echo 'Bottle rocket.  Bottled juices.  Bottled beer.' | awk -v s="Bottle" '{for(i=1; i<=NF; i++) if($i==s) $i=""}1'

produces:
Code:
 rocket. Bottled juices. Bottled beer.

removing one space after the first two periods in the input string.

It isn't clear from the skimpy description of the desired results in this thread whether or not this is allowable. But, the request was for sed solutions only...
# 7  
Old 03-19-2015
Yes, I was suggesting an alternative to sed. The space squeezing could be avoided by using
Code:
awk -F '[ ]' ...

but that only works if there are no TABs..


--
In the case of your sed solution TABs can be factored in by using [[:blank:]] instead of space:
Code:
sed -e 's/^Bottle\([[:blank:]]\)/\1/' -e 's/\([[:blank:]]\)Bottle$/\1/' -e 's/^Bottle$//' -e 's/\([[:blank:]]\)Bottle\([[:blank:]]\)/\1\2/g'


--
There is a spurious leading / -e '/s/^Bottle$//'

--
But then there is still a problem with a text like foo Bottle Bottle bar

Last edited by Scrutinizer; 03-19-2015 at 06:30 AM..
This User Gave Thanks to Scrutinizer For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Match exact String with sed command

I have a workaround to the problem i m posting, however if someone wants to look at my query and respond ... i will appreciate. This is in reference to this thread -> https://www.unix.com/shell-programming-and-scripting/267630-extract-between-two-exact-matched-strings.html I have data.txt as... (11 Replies)
Discussion started by: mohtashims
11 Replies

3. Shell Programming and Scripting

Grep or sed - printing line only with exact match

Hello. In my script, some command return : q | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | x86_64 | openSUSE-13.2-Kernel_stable_standard | kernel-default | package | 3.19.0-1.1.g8a7d5f9 | i586 | openSUSE-13.2-Kernel_stable_standard | kernel-default ... (3 Replies)
Discussion started by: jcdole
3 Replies

4. Shell Programming and Scripting

Exact match using sed

I would like replace all the rows in a file if a row has an exact match to number say 21 in a tab delimited file. I want to delete the row only if it has 21 any of the rows but it should not delecte the row that has 542178 or 563421. I tried this sed '/\<21\>/d' ./inputfile > output.txt ... (7 Replies)
Discussion started by: Kanja
7 Replies

5. Shell Programming and Scripting

grep and sed exact match questions

This post was previously mistaken for homework, but is actually a small piece of what I working on at work. Please answer if you can. QUESTION1 How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1... (2 Replies)
Discussion started by: thibodc
2 Replies

6. UNIX for Dummies Questions & Answers

grep and sed exact match questions

This was mistaken as homework in a different forum, but is not. These are questions that are close to what I am trying to do at work. QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1... (1 Reply)
Discussion started by: thibodc
1 Replies

7. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

8. Shell Programming and Scripting

sed to match only exact string only in all occurences

Dear Friends, Anybody knows how to match exact lines only in multilinear. Input file: apple orange orange apple apple orange Desired output: fruit orange apple fruit i used the command (1 Reply)
Discussion started by: vasanth.vadalur
1 Replies

9. UNIX for Dummies Questions & Answers

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... (5 Replies)
Discussion started by: risk_sly
5 Replies

10. UNIX for Dummies Questions & Answers

How can I match . (actual dot) using sed?

Hi All, How can I match . (actual dot) using sed? Please help. Thanks (9 Replies)
Discussion started by: jingi1234
9 Replies
Login or Register to Ask a Question