SED to replace exact match, not first occurrence.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED to replace exact match, not first occurrence.
# 1  
Old 01-10-2011
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 case, if I were to replace 3 with 5, my price will be 53 because it replaced the 3 which comes first)
Is there a way to only replace data within the Delimiter?
Or rather, how should I go about solving this?

Thanks in advance!
# 2  
Old 01-10-2011
Code:
$ cat file1
(Product:Price:QuantityAvailable) (: as delimiter)
Chocolate:5:5
Banana:33:3

$ sed "/^Chocolate/ s/[^:]*$/14/" file1
(Product:Price:QuantityAvailable) (: as delimiter)
Chocolate:5:14
Banana:33:3

Other languages, like awl handle "delimeters" better than sed.
This User Gave Thanks to Scott For This Post:
# 3  
Old 01-10-2011
Depending on how exactly you want to edit:
If you want to change all line having "Chocolate:5:5" to "Chocolate:5:10"
Code:
sed 's/Chocolate:5:5/Chocolate:5:10/'

OR change all lines "Banana:33:3" to "Banana:33:5"
Code:
sed 's/Banana:33:3/Banana:33:5/'

OR change line Banna pricers to 5 (whatever be the current price)
Code:
sed 's/Banana:\(.*\):\(.*\)/Banana:\1:5/'

This User Gave Thanks to anurag.singh For This Post:
# 4  
Old 01-10-2011
Thanks guys,
another question here. (Sorry, I just started learning bash programming last week)

but what if my data was like this:
Chocolate:12:12:12:12:12:12:12

say I want to replace the 4th 12 to 5 without changing the rest of the 12 infront of it? If I were to code it the way using start with Chocolate:12:12:12: and start replacing, wouldn't it be problematic if my files were to have alot data?
I'm not restricted to using SED, but restricted to using BASH.
Are there any suggestions?

Thanks alot.
# 5  
Old 01-10-2011
Well, I never saw that question coming Smilie Smilie

sed starts getting ugly when you try to do this stuff.

Code:
$ awk -F: '/^Chocolate/ {$4=5}1' OFS=: file1
Chocolate:12:12:5:12:12:12:12

This User Gave Thanks to Scott For This Post:
# 6  
Old 01-10-2011
Really thanks and appreciate your fast reply!
I'll give the code a try later. (currently learning to do other functions)

Thanks again!
# 7  
Old 01-10-2011
Not that ugly Smilie
Code:
sed '/^Chocolate/s/[^:]*/5/5' file

(4th 12 is the fifth field, right?)
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

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. UNIX for Dummies Questions & Answers

Code for exact match to count occurrence

Hi all, I have an input file as below. I would like to count the occurrence of pattern matching 8th field for each line. Input: field_01 field_02 field_03 field_04 field_05 field_06 field_07 field_08 TA T TA T TA TA TA... (3 Replies)
Discussion started by: huiyee1
3 Replies

3. UNIX for Dummies Questions & Answers

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. echo "Bottle BottleWater Bottle Can" | sed 's/\<Bottle\>//g' BottleWater CanBut if my data has a dot or hash in it, it replaces all the... (10 Replies)
Discussion started by: grep_me
10 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. UNIX for Dummies Questions & Answers

[Solved] Replace first occurrence after match

hey guys, i have been trying to work this thing out with sed with no luck :confused: i m looking for a way to replace only the first occurrence after a match for example : Cat Realized what you gotta do Dog Realized what you gotta do Sheep Realized what you gotta do Wolf Realized... (6 Replies)
Discussion started by: boaz733
6 Replies

6. 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

7. 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

8. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

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 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

9. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

10. 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
Login or Register to Ask a Question