HELP with awk or sed. Need to replace all IP address by 2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HELP with awk or sed. Need to replace all IP address by 2
# 1  
Old 12-21-2012
HELP with awk or sed. Need to replace all IP address by 2

Hi,

In a file, I have several time

Code:
    <IP>232.0.1.164</IP>
...        
    <IP>232.0.1.135</IP>

I need to replace all the random IP addresses , by 239.0.0.1 and 239.0.0.2 , alternatively.

I try this
Code:
grep "<IP>" tsp.xml | awk '{if(NR % 2)print $0}' | cut -d"<"  -f2 | cut -d">" -f2

It gives all the IP Address of the Odd occurences, but I don't know how to substitute it in the file.

I see also the gsub function of awk but I lost.

Can you help me ?

Best Regards,
Frédéric

Last edited by Scrutinizer; 12-21-2012 at 11:00 AM.. Reason: code tags
# 2  
Old 12-21-2012
try:
Code:
awk 'BEGIN {a[1]="239.0.0.1"; a[0]="239.0.0.2"} /<IP>/ {sub(">.*<",">" a[NR %2] "<")} 1 ' tsp.xml

# 3  
Old 12-22-2012
Thanks you rdrtx1,

With your command the half first part of the IP is replaced by 239.0.0.2 and the second part is 239.0.0.1

Is it possible to have a switching, for the Odd occurrences the IP 239.0.0.1 and for the Even occurrences the IP 238.0.0.2 ?

Regards,

---------- Post updated at 08:26 AM ---------- Previous update was at 08:15 AM ----------

It seems NR , do the modification in function of the line number and not the occurrence number.

It seems, In the first part the "grep <IP>" is on Odd line number and in the second part on Even

---------- Post updated at 08:39 AM ---------- Previous update was at 08:26 AM ----------

This Command works

Code:
awk 'BEGIN {a[1]="239.0.0.1"; a[0]="239.0.0.2" ; n=0} /<IP>/ {sub(">.*<",">" a[n%2] "<"); n++}; 1 ' tsp.xml

Thanks for your support

Last edited by Scott; 12-22-2012 at 09:43 AM.. Reason: Please use code tags
# 4  
Old 12-22-2012
You may want to try this one:
Code:
$ awk '/<IP>/ {sub(">.*<",">239.0.0"n++%2+1"<")}1' tsp.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace my perl with awk or sed

My code below will print only the email address from all lines. I want to convert it with sed or awk.. also what if i just want to find only filenames. cat LIS_EMAIL | perl -wne'while(/+@+\w+/g){print "$&\n"}' Hoping to extract the filename such us .exe, .bin. From file that has scrambled... (8 Replies)
Discussion started by: invinzin21
8 Replies

2. UNIX for Beginners Questions & Answers

Replace using sed or awk

Hi, Need a help to replace a word if a pattern is found between the delimiters preferably using SED or AWK. below is the sample file that iam dealing with, need to match pattern 'application' if found replace the whole word between the delimiters and also print the lines that don't match.... (1 Reply)
Discussion started by: tech_frk
1 Replies

3. Debian

Using awk and sed to replace text

Good Day Every one I have a problem finding and replacing text in some large files that will take a long time to manually edit. Example text file looks like this #Example Large Text File unix linux dos squid bind dance bike car plane What im trying to do is to edit all the... (4 Replies)
Discussion started by: linuxjunkie
4 Replies

4. Shell Programming and Scripting

IP Address Modification through awk/sed

Hi, I have to modify the 2nd and 3rd octet of the IP address through awk/sed. For Example: Given IP is : 10.205.22.254, it should be modified as 10.105.100.254 through awk/sed. Kindly help me on this and let me know if you have any questions. Thanks in advances. (2 Replies)
Discussion started by: kumarbka
2 Replies

5. Shell Programming and Scripting

sed and awk -Find and Replace

All, I have thousands of lines in a file with following format DATA=_ONE_XXX_YYY_CCC_HHHG_ DATA1=_GGG_JJJJ_HHH_UUU_JJJJ_HHHH_LLL_ DATA3=_MMM_GG_NN_QQQQ_FFF_III_ I want to replace _ with . by ignoring the first (=_) and last (_) So that out put should looks like... (4 Replies)
Discussion started by: baluchen
4 Replies

6. Shell Programming and Scripting

Get Local IP address using Sed, Awk

Hi All, how to get solaris box local ip addresss in variable, using sed or awk utlities. Thanks, Mani Muthu (7 Replies)
Discussion started by: k_manimuthu
7 Replies

7. UNIX for Dummies Questions & Answers

replace 0.00 with awk/sed

Hi I have a problem when i use awk or sed to replace characters in file. For example when I want to replace line like this : 00000O120100512 1.70 1.59 0.00 +7.280 I want to get a new line : 0000000O120100512 1.70 1.59 13.56 +7.280 In ksh : awk... (1 Reply)
Discussion started by: Artur
1 Replies

8. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies

9. Shell Programming and Scripting

Sed help to replace and then awk

Sed help echo "(200 rows affected)" | sed -e '/\(//p' | sed -e '/\)//p' | awk '{print $1}' I want output as "200" Please help me correct (2 Replies)
Discussion started by: pinnacle
2 Replies

10. Shell Programming and Scripting

Replace tr/sed with awk

There is a service that runs that we call multi-streaming that calls a shell script multiple times simultaneously. In this shell script is the following line: tr '\r' '\n' < $POLLFILE.OUT | sed '/0000000000000016000A/d' > $POLLFILE When I run this manually it produces the desired results, but... (6 Replies)
Discussion started by: philplasma
6 Replies
Login or Register to Ask a Question