replace a similar field in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace a similar field in a file
# 1  
Old 11-01-2006
replace a similar field in a file

Hello,
I am having a file where I have to replace the port values with the variable I defined.
The file is an extract of an xml file:
<NameValuePair>
<name>Service1</name>
<value>tcp:32406</value>
</NameValuePair>
<NameValuePair>
<name>Service2</name>
<value>32406</value>
</NameValuePair>
<NameValuePair>
<name>Service3</name>
<value>tcp:32480</value>
</NameValuePair>
<NameValuePair>
<name>Service4</name>
<value>32480</value>
</NameValuePair>

I have Port1=7001 ; Port2=7011 ; I need to replace Port1 for Service1 and Service2 ports and Port2 for Service3 and Service4.
I know to replace it with `sed` but I am not able to grep for that <value> line as there are multiple places. The services are unique but not sure how to go to the next <value> string associated with the Service.

I need help and really appreciate your ideas.
Chiru
# 2  
Old 11-01-2006
Code:
sed "/Service1/{n;s/[0-9][0-9]*/$Port1/;}" file

# 3  
Old 11-01-2006
Thanks Anbu, it's working!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace field in the delimited file

Hi, I have the requirement similar to the one mentioned in the below thread. https://www.unix.com/unix-for-dummies-questions-and-answers/128155-search-replace-string-only-particular-column-delimited-file.html The only difference is that I need to change the field for row 1,2 and the last... (14 Replies)
Discussion started by: chetanojha
14 Replies

2. Shell Programming and Scripting

Need to replace last field in a file,if first field matches

Hi, Need to replace last field in a file(/etc/passwd) ,if first filed matches with particular username. Scenario: cat testfor1 deekshi:x:7082:7082::/home/deekshi:/bin/bash harini1:x:7083:7083::/home/harini1:/bin/bash Here,if first field contains "deekshi", then i should replace... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

3. UNIX for Dummies Questions & Answers

Combine Similar Output from the 2nd field w.r.t 1st Field

Hi, For example: I have: HostA,XYZ HostB,XYZ HostC,ABC I would like the output to be: HostA,HostB: XYZ HostC:ABC How can I achieve this? So far what I though of is: (1 Reply)
Discussion started by: alvinoo
1 Replies

4. Shell Programming and Scripting

Replace pattern from nth field from a file

$ cat /cygdrive/d/Final2.txt 1,A ,Completed, 07.03_23.01 ,Jun 30 20:00 2,BBB,Pending,, 3,CCCCC,Pending,, 4,DDDDD,Pending,, 5,E,Pending,, 6,FFFF,Pending,, 7,G,Pending,, In the above file 4th field is date which is in MM.DD_HH.MIN format and I need to convert it to as it is there in 5th... (1 Reply)
Discussion started by: Amit Joshi
1 Replies

5. Shell Programming and Scripting

Replace field in one file with whole record data of another

Hello Group, I need to replace the city field in “File 1 (fileld 3), with the entire record line of “File 2” (including delimiters) where the “city” field (File 1, Field 3)matches city field (File 2, Field1). All of the other data in “File 1” should remain intact(Fields 1,2,4,5,6). Only field... (1 Reply)
Discussion started by: vestport
1 Replies

6. Shell Programming and Scripting

Replace first field with Rownum in a file

Hi, I've a file which is "|" delimited. I want to replace the first field with rownum, how can I do it with awk or sed command? There is no header record. sample file 1195|ABC|DEF|30 LATE DR||CHARLOTTE|NC||NONE@NONE.COM|I|1 1227|PQR|STU|3 KING DR||LIVONIA|NJ|481524071|YAHOO@YAHOO.COM|I|3... (4 Replies)
Discussion started by: rudoraj
4 Replies

7. Shell Programming and Scripting

Replace a particular field in all records in a csv file

hi, i have various csv files, the file format is as follows Entry: "1",4,2010/08/15-10-00-00.01,,"E",,,,,,,,,120,0,"M4_","C","KEW-011-5337140-20100916163456-540097","1234567890","N N 0 ",,,"NUK 800100200",,,"NN",,,,,,,,,,,,"0000000001|0001|20150401... (2 Replies)
Discussion started by: niteesh_!7
2 Replies

8. Shell Programming and Scripting

Replace third field of the first record in a file....

Hi, I am new to unix and am trying to do something below: I have a pipe delimited file with millions of records. I need to replace the third column of the first record to the number of lines in the file. How can I do that. Will appreciate any advice and help. Thanks Simi (3 Replies)
Discussion started by: simi28
3 Replies

9. Shell Programming and Scripting

Help to replace a field in one file with a field from another file

I have to replace a field in one file with a field from other file. I came across this awk command to replace a field with one string nawk -F'|' -v OFS='|' '$2="replace"' temp2 > temp3 I need to have something like cut -f2 -d "|" temp1 (a field from other file) instead of 'replace' Is... (8 Replies)
Discussion started by: savant
8 Replies

10. Shell Programming and Scripting

replace a field in a CSV file

Hello all, I've a CSV file and need to replace 5th field if its value is "X". The exact requirement is to replace 5th field (column) with "Y" if a. it's value is "X" AND b. the line must start with ABC string i guess this can be done with awk. Pl help. For security reasons, the... (2 Replies)
Discussion started by: prvnrk
2 Replies
Login or Register to Ask a Question