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
# 8  
Old 03-19-2015
Another technique:
add extra delimiters to the beginning and end,
then do a global substitution with delimiters attached and re-substituted,
then remove the extra delimiters.
Code:
echo "Bottle Bottle.Water Bottle Can" |
sed '
s/^/ /; s/$/ /
s/ Bottle /  /g
s/^ //; s/ $//
'

This User Gave Thanks to MadeInGermany For This Post:
# 9  
Old 03-19-2015
How about (assuming your sed has EREs):
Code:
sed -E 's/Bottle([[:space:]]|$)//g' <<<"Bottle Bottle.Water Bottle Can Bottle"
Bottle.Water Can

# 10  
Old 03-19-2015
Quote:
Originally Posted by RudiC
How about (assuming your sed has EREs):
Code:
sed -E 's/Bottle([[:space:]]|$)//g' <<<"Bottle Bottle.Water Bottle Can Bottle"
Bottle.Water Can

That should then be:

Code:
sed -E 's/(^|[[:space:]])Bottle([[:space:]]|$)/\1\2/g' <<<"Bottle Bottle.Water Bottle Can Bottle"

Which is what Don was referring to in post #4

--
But a problem with these sed approaches is when there is foo Bottle Bottle bar, then it still does not work correctly, something that the awk approach avoids..

-- Edit: --
Perhaps this perl:
Code:
perl -pe 's/(^|(?<=\s))Bottle(?=(\s|$))//g'


Last edited by Scrutinizer; 03-19-2015 at 06:38 AM..
# 11  
Old 03-19-2015
The look-ahead is sufficient:
Code:
echo Bottle foo Bottle Bottle Bottle.Water bar Water.Bottle Bottle |
perl -pe 's/(^|\s)Bottle((?=\s)|$)/$1/g'

The $1 puts back a leading space (omit if not desired).
 
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