-3

I have a file :

# Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,80,110,143,443,4433,465,587,993,995,3306,9200,8080,8888,10000:20000,"

# Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,53,80,110,113,143,443,465,4433,587,993,995,3306,9200,8080,8888,10000:20000,"

# Allow incoming UDP ports
UDP_IN = "20,21,53,111"

# Allow outgoing UDP ports
# To allow outgoing traceroute add 33434:33523 to this list 
UDP_OUT = "20,21,53,113,123,111"

# Allow incoming PING. Disabling PING will likely break external uptime
# monitoring
ICMP_IN = "1"

I want to delete, lets say 443 from line starting with TCP_OUT = " but not from anywhere else in the file.

a sed or awk solution would be what I am looking for.

Clarification : I don't want to delete the entire line that starts with a specific string, I want to delete a sub string in a line that starts with a string. ex : I want to delete just the 443 in line that starts with TCP_OUT = " I don't want to delete entire line that starts with TCP_OUT = "

Srikanth Koneru
  • 264
  • 1
  • 3
  • 13
  • maybe [this](https://unix.stackexchange.com/questions/374127/sed-delete-the-line-start-with-but-not-with-shell-scripts) or [that](https://stackoverflow.com/questions/5410757/delete-lines-in-a-text-file-that-contain-a-specific-string) can help. – TomBombadil Sep 06 '18 at 09:32
  • @TomBombadil those are not want I want, I added some clarification – Srikanth Koneru Sep 06 '18 at 09:39
  • Yes I thought you might be able to combine the various information to what you want. I'm sry if it does not help. – TomBombadil Sep 06 '18 at 09:44
  • 1
    Please avoid *"Give me the codez"* questions. Instead show the script you are working on and state where the problem is. Also see [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/608639) – jww Sep 07 '18 at 01:08

3 Answers3

0

Use an "address" in sed to run a substitution only on a particular line:

sed '/^TCP_IN /s/\([",]\)443[",]/\1/'

The [",] are needed to correctly handle the cases when 443 is the first or last item in the list.

choroba
  • 231,213
  • 25
  • 204
  • 289
0

You may use this sed command:

sed -iE '/^TCP_OUT /s/"443"/""/; s/([",])443,|,443([,"])/\1\2/' file

This sed checks for all of the 4 possible cases:

  • When 443 is the only number in input i.e. "443"
  • When 443 is at the start i.e. "443,111,222,333"
  • When 443 is at the end i.e. "987,546,443"
  • When 443 is in the middle i.e. "111,222,333,443,678,123,098"
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

With sed:

sed '/^TCP_OUT/ s/443,*//' file
Cyrus
  • 84,225
  • 14
  • 89
  • 153