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.
# 8  
Old 01-10-2011
Quote:
Originally Posted by Scrutinizer
Not that ugly Smilie
Code:
sed '/^Chocolate/s/[^:]*/5/5' file

(4th 12 is the fifth field, right?)
You're right. I never remember that thing Smilie
This User Gave Thanks to Scott For This Post:
# 9  
Old 01-10-2011
Quote:
Originally Posted by Scrutinizer
Not that ugly Smilie
Code:
sed '/^Chocolate/s/[^:]*/5/5' file

(4th 12 is the fifth field, right?)
yes, 4th 12 is fifth, but can you explain this code? I can't really understand it.

I understand the part sed '/^Chocolate/s
It means Start With Chocolate, and then substitute right?
What I don't understand is the [^:]*/5
The last part /5 means replace with 5 right?

As for $ awk -F: '/^Chocolate/ {$4=5}1' OFS=: file1
What does the "{$4=5}1" , the 1 means?

I'll give both codes a try later just to learn more.
Thanks!
# 10  
Old 01-10-2011
Quote:
Originally Posted by Scrutinizer
Not that ugly Smilie
Smilie
Code:
sed '/^Chocolate/{/:/s/12/5/4}' inputfile > outfile

Change the last number (here) 4, according to the position of 12 you want to change
This User Gave Thanks to michaelrozar17 For This Post:
# 11  
Old 01-10-2011
It means on the line starting with Chocolate replace the first occurrence of the pattern with 5. The pattern is [^:]*, which means 0 or more occurrences (*) of any character that isn't ":"

---
@michael, that is equivalent to:
Code:
sed /^Chocolate/s/12/5/4 file

But then you would really use the content as the fields instead of just the position. That would really change the 4th occurrence of 12 irrespective of what column it is in, or what string it is part of.

Last edited by Scrutinizer; 01-10-2011 at 08:28 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 01-10-2011
Thanks scruti for letting me know that. I thought giving the 2nd pattern match colon : (here) makes the sed to handle the input lines as : delimited.
# 13  
Old 01-10-2011
Quote:
Originally Posted by andylbh
What does the "{$4=5}1" , the 1 means?
PatternActionMeaning
/^Chocolate/{$4=5}For a line starting with "Chocolate", set the fourth field to "5"
1-1 evaluates to true. If no action is specified the default action is to print the line (shorthand for { print })
This User Gave Thanks to Scott For This Post:
# 14  
Old 01-10-2011
@michael

It would mean if the line starts with Chocolate then if the line contains a colon : then substitute.....
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