Repeative Output

Folks,

Not got much scritping experience, so I expect this will be a fairly trivial fix for those that know what they're doing...

I've got two files:

root[my-box]# cat /tmp/exemption_list
# This file will contain comments

# and blank lines.

aaa.bbb.ccc.000
aaa.bbb.ccc.222
aaa.bbb.ccc.444
aaa.bbb.ccc.666
root[my-box]#
root[my-box]# cat /tmp/subnet_list
aaa.bbb.ccc.000
aaa.bbb.ccc.111
aaa.bbb.ccc.222
aaa.bbb.ccc.333
aaa.bbb.ccc.444
aaa.bbb.ccc.555
aaa.bbb.ccc.666
aaa.bbb.ccc.777
aaa.bbb.ccc.888
aaa.bbb.ccc.999
root[my-box]#

What I'm trying to do, is run through the subnet_list and pick out and remove all of the matching addresses that exist in the exemption list and then write the amended list back to subnet_list - so essentially I'll end up with a list of subnets minus the exempt ones. Exemption list is something that will be ever changing, as a result I can't hard code it into the script.

The closest I've been able to get is:

root[my-box]# egrep -v "#|^$" /tmp/exemption_list| while read line; do grep -v $line /tmp/subnet_list >> /tmp/subnet_list_result; done
root[my-box]# mv /tmp/subnet_list_result /tmp/subnet_list

Which isn't quite right at all...

Can anyone help tweak the egrep command so I end up with a subnet_list file containing no exempt addresses and no duplicates?

Thanks in advance.
CiCa

Hi,
most grep implementations can read its pattern from a file:

grep -vf/tmp/exemption_list /tmp/subnet_list |sort -u >/tmp/subnet_list_result
mv /tmp/subnet_list_result /tmp/subnet_list

With bash or ksh93, you can do something like:

grep -vxFf <(grep -Ev "#|^$" file1) file2

Some greps can do this:

grep -Ev "#|^$" file1 | grep -vxFf - file2

otherwise you could use intermediate files, or create an awk script to do it...

1 Like

I tried something similar a day or two ago, but -F or -f ain't an option on my system

root[my-box]# grep -vF /tmp/exemption_list /tmp/subnet_list
grep: illegal option -- F
Usage: grep -hblcnsviw pattern file . . .
root[my-box]# grep -vf /tmp/exemption_list /tmp/subnet_list
grep: illegal option -- f
Usage: grep -hblcnsviw pattern file . . .
root[my-box]#

Are there any alternative methods?

Try /usr/xpg4/bin/grep

1 Like

Brilliant!

root[my-box]# /usr/xpg4/bin/grep -vxFf <(grep -v "#|^$" /tmp/exemption_list) /tmp/subnet_list
aaa.bbb.ccc.111
aaa.bbb.ccc.333
aaa.bbb.ccc.555
aaa.bbb.ccc.777
aaa.bbb.ccc.888
aaa.bbb.ccc.999
root[my-box]

#

Thanks everyone that responded (in particular Scrutinizer) :slight_smile: