Change specific occurence with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change specific occurence with sed
# 8  
Old 05-17-2010
Another approach Smilie:
Code:
awk -F"=" -v var="6677" -v count ="3" '
/\[Sdp]\/{count++}
/^port/ && count==3 {$2=var}
1' OFS="=" file

# 9  
Old 05-17-2010
MySQL

Code:
sed '/\[Sdp\]/{;n;n; s/port=2233/port=6677/;p;d;q}' file

# 10  
Old 05-18-2010
Thx all for your responses, but ygemici, I tried yours and it returns an error for me ...

Code:
$ sed '/\[Sdp\]/{;n;n; s/port=2233/port=6677/;p;d;q}' inputfile
sed: command garbled: /\[Sdp\]/{;n;n; s/port=2233/port=6677/;p;d;q}

I'm using a :
SunOS 5.10 Generic_118833-36 sun4u sparc SUNW,Sun-Fire-V240

Regs,
# 11  
Old 05-18-2010
The solution from ygemici is missing a semi-colon:

Code:
sed '/\[Sdp\]/{;n;n; s/port=2233/port=6677/;p;d;q;}'

In any case it assumes that the port is the third line after the [Std] tag and that the value of port is fixed.
# 12  
Old 05-18-2010
ok, thx a lot scottn ...
# 13  
Old 05-19-2010
Quote:
Originally Posted by scottn
The solution from ygemici is missing a semi-colon:

Code:
sed '/\[Sdp\]/{;n;n; s/port=2233/port=6677/;p;d;q;}'

In any case it assumes that the port is the third line after the [Std] tag and that the value of port is fixed.
Hmm I forgot it but it isnt necessity gnu sed which i use Smilie
I think SunOS sed v4.1 or SunOS sed v5.6 is needed


Code:
[root@rhnserver ~]# cat yap
[Batch]
aaa
port=1234
time
[Sdp]
bbb
port=2233
name
[Ivr]
ccc
port=4444
name

Code:
[root@rhnserver ~]# sed '/\[Sdp\]/{;n;n; s/port=2233/port=6677/;p;d;q}' yap
[Batch]
aaa
port=1234
time
[Sdp]
bbb
port=6677
name
[Ivr]
ccc
port=4444
name

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