Change specific occurence with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change specific occurence with sed
# 1  
Old 05-17-2010
Change specific occurence with sed

Hello,

I have this file.

Code:
[Batch]
aaa
port=1234
time
[Sdp]
bbb
port=2233
name
[Ivr]
ccc
port=4444
name


Is there any way with sed to change only the occurence of "port" which comes after section [sdp] to have as output :

Code:
[Batch]
aaa
port=1234
time
[Sdp]
bbb
port=6677
name
[Ivr]
ccc
port=4444
name


It means, can we say to sed, change only this expression that comes after this expression.
Else is there a way to tell sed to change for intance only the second or the third occurence of a pattern of a file.

Thanks ...
# 2  
Old 05-17-2010
Code:
$ sed "/\[Sdp\]/,/\[/ s/^port=.*/port=6677/" file1
[Batch]
aaa
port=1234
time
[Sdp]
bbb
port=6677
name
[Ivr]
ccc
port=4444
name

# 3  
Old 05-17-2010
Thx a lot for this very quick response ...

Just one thing please, how can we do this by saying to sed for instance, I want to change only the third occurence in the file ?

Thx in advance ...
# 4  
Old 05-17-2010
Probably with some difficulty.

I'd use awk for that.
# 5  
Old 05-17-2010
ok, thank you ...
# 6  
Old 05-17-2010
You're welcome!

Not so elegant, but...
Code:
awk -v N=3 '/\[/ { P = 0 } /\[Sdp\]/ { C++; P = 1 } /^port=/ && C == N && P { $0 = "port=6677" } 1' file1

(change N=3) to which ever occurrence you want...)
# 7  
Old 05-17-2010
Thx a lot ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

Using sed to change values after a specific string

Hello I have a script that searches a file for a specific string and then changes the nth column after that string. I have searched online for how to do this with sed but have not seemed to find a solution that works for me. I am using bash. Some background info: - Currently I am using awk to... (4 Replies)
Discussion started by: prodigious8
4 Replies

3. Shell Programming and Scripting

Add character to specific columns using sed or awk and make it a permanent change

Hi, I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches. file has lot of functions defined a.sh #!/bin/bash fn a { beautiful evening sunny day } fn b { } fn c { hello world .its a beautiful day ... (12 Replies)
Discussion started by: ashima jain
12 Replies

4. Shell Programming and Scripting

start searching a word from the particular record on the result of first occurence change the value

Hi, I need a script to start searching a word from the particular record on the result of first occurence i need to change the value in that record. I have a input file like this <properties> <add key="DeliveryWithinDay" value="False" /> <add key="ABC" value="23:00:00 PM" /> <add... (5 Replies)
Discussion started by: NareshN
5 Replies

5. Shell Programming and Scripting

sed second occurence error

Hi Guys, i'm trying to use sed to replace the second occurrence of the 3 digit number in an I.P address and replace it with another value but it errors out. please advise $ cat infil 3538,,,,,,ID,ID1,,,,,,,,,,,123.110.24.5 sed 's/\{1,3\}\./x\./\2/' infil sed: -e expression #1, char... (2 Replies)
Discussion started by: Irishboy24
2 Replies

6. Shell Programming and Scripting

Number each occurence using sed

hi, I need to number each occurrence of a pattern within a file using sed. Given object 0000 object 111 object 222 I need following 1.object 0000 2.object 111 3.object 222 (5 Replies)
Discussion started by: xerox
5 Replies

7. UNIX for Dummies Questions & Answers

Search specific pattern in file and return number of occurence

Hi I want to search for a specific pattern in file Say ABC;HELLO_UNIX_WORLD;PQR ABC;HELLO_UNIX_WORLD_IS_NOT_ENOUGH;XYZ ABC;HELLO_UNIX_FORUM;LMN Pattern to search is : "HELLO_UNIX_*****" and not "HELLO_UNIX_***_***_" I mean after "HELLO_UNIX" there can only be one word.In this case... (2 Replies)
Discussion started by: dashing201
2 Replies

8. Shell Programming and Scripting

Change first occurence

I'm not getting the syntax correct to change a line only on the first occurrence: I've tried to change only the first match and I've tried to change the from the second match forward sed 's/<B>PT#/<tr><td class=\"pt1\" width=\"40%\"><B>pt#/1' $file > tmpfile.html sed ... (0 Replies)
Discussion started by: dba_frog
0 Replies

9. UNIX for Dummies Questions & Answers

Using SED to change a specific word's color?

Ok so all i'm trying to do here is output a file and change the color of a specific word. I can't use grep with color because I need all lines of the file not just lines that match the pattern. I can get this substitution to work but when it displays it shows exactly what i'm putting it rather... (14 Replies)
Discussion started by: MrEddy
14 Replies

10. Shell Programming and Scripting

SED 4.1.4 - INI File Change Problem in Variables= in Specific [Sections] (Guru Help)

GNU sed version 4.1.4 on Windows XP SP3 from GnuWin32 I think that I've come across a seemingly simple text file change problem on a INI formatted file that I can't do with SED without side effects edge cases biting me. I've tried to think of various ways of doing this elegantly and quickly... (5 Replies)
Discussion started by: JakFrost
5 Replies
Login or Register to Ask a Question