The lines in the file :
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2000 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2001 -j ACCEPT
-A
To complement @Avinash Raj's helpful answer with a more generic, POSIX-compliant solution.
Note that the solution is awk
-based, because a robust portable solution with sed
is virtually impossible due to the limitations of POSIX' basic regular expressions.
awk -v commentId='#' -v word='2001' '
$0 ~ "(^|[[:punct:][:space:]])" word "($|[[:punct:][:space:]])" {
if (match($0, "^[[:space:]]*" commentId))
$0 = substr($0, RSTART + RLENGTH)
else
$0 = commentId $0
}
{ print }
' file > tmpfile.$$ && mv tmpfile.$$ file
(^|[[:punct:][:space:]])
and ($|[[:punct:][:space:]])
are the POSIX extended regex equivalents of the \<
and \>
word-boundary assertions known from other regex dialects.awk
doesn't offer in-place updating (neither does POSIX sed
, incidentally), hence the output is first captured in a temporary file and that file then replaces the original on success.