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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete multiple occurrences of the same pattern on a line but the first
# 1  
Old 09-26-2018
Delete multiple occurrences of the same pattern on a line but the first

The lines that I am trying to format look like
Code:
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.
Code:
sed -i -e 's/,      IP address: / /g' -e 's/,      IP address: / /1' $tempdir/newt4

This is the intended output
Code:
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:
Code:
sed -ri 's/(\IP address:)(,      IP address:)(Platform:)/\1\3/g'

This just removed all instances

Last edited by vgersh99; 09-26-2018 at 04:56 PM.. Reason: fixed code tag
# 2  
Old 09-26-2018
a bit verbose, but A start.
awk -f dis.awk myFile where dis.awk is:
Code:
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
}

# 3  
Old 09-26-2018
With a little modification you can make it work:
Code:
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.
These 4 Users Gave Thanks to MadeInGermany For This Post:
# 4  
Old 09-26-2018
How about
Code:
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,

This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-27-2018
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:
Code:
sed -i 's/,	  IP address: /,IP Address:/' $tempdir/newt4 
sed -i -e 's/,	  IP address: / /g' $tempdir/newt4

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

2. Shell Programming and Scripting

Multiple pattern find and delete line

I have a file # cat /tmp/user_find.txt /home/user/bad_link1 /home/user/www /home/user/mail /home/user/access_logs /home/user/bad_link2 I need to delete lines where there are patterns /home/user/www, /home/user/mail and /home/user/access_logs. I used below method, but its throwing error... (8 Replies)
Discussion started by: anil510
8 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Homework & Coursework Questions

sed Multiple Pattern search and delete the line

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have file which has got the following content sam 123 LD 41 sam 234 kp sam LD 41 kam pu sam LD 61 Now... (1 Reply)
Discussion started by: muchyog
1 Replies

5. Shell Programming and Scripting

awk delete/remove rest of line on multiple search pattern

Need to remove rest of line after the equals sign on search pattern from the searchfile. Can anybody help. Couldn't find any similar example in the forum: infile: 64_1535: Delm. = 86 var, aaga 64_1535: Fran. = 57 ex. ccc 64_1639: Feb. = 26 (link). def 64_1817: mar. = 3/4. drz ... (7 Replies)
Discussion started by: sdf
7 Replies

6. Shell Programming and Scripting

CSV: Replacing multiple occurrences inside a pattern

Greatings all, I am coming to seek your knowledge and some help on an issue I can not currently get over. I have been searching the boards but did not find anything close to this matter I am struggling with. I am trying to clean a CSV file and make it loadable for my SQL*Loader. My problem... (1 Reply)
Discussion started by: OCanada
1 Replies

7. Shell Programming and Scripting

sed replace multiple occurrences on the same line, but not all

Hi there! I am really enjoying working with sed. I am trying to come up with a sed command to replace some occurrences (not all) in the same line, for instance: I have a command which the output will be: 200.300.400.5 0A 0B 0C 01 02 03 being that the last 6 strings are actually one... (7 Replies)
Discussion started by: ppucci
7 Replies

8. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

9. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

10. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies
Login or Register to Ask a Question