With
cp /tmp/exclude_list /tmp/exclude_list.tmp
grep -Fx -v -e "$host" /tmp/exclude_list.tmp >/tmp/exclude_list
rm /tmp/exclude_list.tmp
you would remove each line that exactly matches the string in $host
. Change -Fx
to just -F
to remove any line that contains the string. Don't use -F
if you want to use $host
as a regular expression.
Or with Perl:
perl -i -sn -e 'print unless /$host/' -- -host="$host" /tmp/exclude_list
The options used with perl
here is -i
for in-place editing, -s
to allow Perl to instantiate the $host
Perl variable from the command line, and -n
to only print explicitly from within the implicit loop that Perl provides around the code. The -e
takes the actual code as the argument.
The Perl code would remove all lines that does not match the regular expression $host
.
To use $host
as a string:
perl -i -sn -e 'print unless index($_, $host) >= 0' -- -host="$host" /tmp/exclude_list
grep -v
is the simplest way to do it.