Delete multiple occurrences of the same pattern on a line but the first

The lines that I am trying to format look like

Device ID: j01-01,      IP address: 10.10.10.36,      IP address: 10.10.10.35,      IP address: 10.10.102.201,    Platform: 8040,  Capabilities: Host ,
Interface: GigabitEthernet9/45,  Port ID (outgoing port): e0k,

Here is what I have so far but it didn't work.

sed -i -e 's/,      IP address: / /g' -e 's/,      IP address: / /1' $tempdir/newt4

This is the intended output

Device ID: j01-01,  IP address: 10.10.10.36 10.10.10.35 10.10.102.201,    Platform: 8040,  Capabilities: Host ,
Interface: GigabitEthernet9/45,  Port ID (outgoing port): e0k,    

------ Post updated at 07:39 PM ------

I also tried:

sed -ri 's/(\IP address:)(,      IP address:)(Platform:)/\1\3/g' 

This just removed all instances

a bit verbose, but A start.
awk -f dis.awk myFile where dis.awk is:

BEGIN {
   OFS=FS=","
   PATip="IP address:"
}
{
   s=ip=""

   for( i=1;i<=NF; i++) {
     if($i !~ PATip) {
        if (ip) {
           s=(s)?s OFS $i OFS ip:$i
           ip=""
        }
        else
           s=(s)?s OFS $i:$i
     }
     else {
        thisIP=substr($i,index($i,":")+1)
        ip=(ip)?ip " " thisIP:PATip " " thisIP
     }
   }
   print s
}

With a little modification you can make it work:

sed -e 's/,      IP address: /,  IP address: /' -e 's/,      IP address: / /g' newt4

Initially you cannot use the /g search because the first string is quite identical with the next occurrences.
Fortunately you want the first string a bit changed in the output, so the trick is to change it first. Then the /g search can be made to only match the next occurrences.

4 Likes

How about

sed '/IP address/ s//&#/; s/, *IP address://g; s/s#:/s:/' file
Device ID: j01-01,      IP address: 10.10.10.36 10.10.10.35 10.10.102.201,    Platform: 8040,  Capabilities: Host ,
Interface: GigabitEthernet9/45,  Port ID (outgoing port): e0k,
1 Like

Nope, didnt do it.

------ Post updated at 01:52 PM ------

MadeInGermany, I used your approach and it works. Thanks for giving me the direction.

Below is the code that I used to complete it:

sed -i 's/,	  IP address: /,IP Address:/' $tempdir/newt4 
sed -i -e 's/,	  IP address: / /g' $tempdir/newt4