|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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: Code:
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]# Code:
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: Code:
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 Last edited by CiCa; 01-28-2013 at 04:49 AM.. Reason: Inserting code tags |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
Hi, most grep implementations can read its pattern from a file: Code:
grep -vf/tmp/exemption_list /tmp/subnet_list |sort -u >/tmp/subnet_list_result mv /tmp/subnet_list_result /tmp/subnet_list |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
With bash or ksh93, you can do something like: Code:
grep -vxFf <(grep -Ev "#|^$" file1) file2 Some greps can do this: Code:
grep -Ev "#|^$" file1 | grep -vxFf - file2 otherwise you could use intermediate files, or create an awk script to do it... |
| The Following User Says Thank You to Scrutinizer For This Useful Post: | ||
CiCa (01-28-2013) | ||
|
#4
|
|||
|
|||
|
I tried something similar a day or two ago, but -F or -f ain't an option on my system Code:
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? |
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Brilliant! Code:
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)
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| script to mail monitoring output if required or redirect output to log file | aix_admin_007 | Shell Programming and Scripting | 4 | 10-22-2012 08:49 PM |
| Awk script to run a sql and print the output to an output file | adept | Shell Programming and Scripting | 4 | 06-10-2012 10:40 AM |
| awk: round output or delimit output of arithmatic string | jelloir | Shell Programming and Scripting | 1 | 06-18-2010 03:07 AM |
| top output for six processes with the same name, output changed from column to row | Bloke | Shell Programming and Scripting | 5 | 06-04-2009 06:02 AM |
| how to make a line BLINKING in output and also how to increase font size in output | mail2sant | Shell Programming and Scripting | 3 | 04-14-2008 07:30 AM |
|
|