Repeative Output | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Repeative Output

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-28-2013
CiCa CiCa is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 7 June 2013, 8:42 AM EDT
Location: UK
Posts: 27
Thanks: 10
Thanked 0 Times in 0 Posts
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  
Old 01-28-2013
cero cero is online now
Registered User
 
Join Date: Aug 2006
Last Activity: 22 July 2014, 11:11 AM EDT
Posts: 360
Thanks: 3
Thanked 64 Times in 59 Posts
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  
Old 01-28-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 July 2014, 11:27 AM EDT
Location: Amsterdam
Posts: 9,278
Thanks: 260
Thanked 2,302 Times in 2,065 Posts
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  
Old 01-28-2013
CiCa CiCa is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 7 June 2013, 8:42 AM EDT
Location: UK
Posts: 27
Thanks: 10
Thanked 0 Times in 0 Posts
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
    #5  
Old 01-28-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 July 2014, 11:27 AM EDT
Location: Amsterdam
Posts: 9,278
Thanks: 260
Thanked 2,302 Times in 2,065 Posts
Try /usr/xpg4/bin/grep
The Following User Says Thank You to Scrutinizer For This Useful Post:
CiCa (01-28-2013)
Sponsored Links
    #6  
Old 01-28-2013
CiCa CiCa is offline
Registered User
 
Join Date: Jun 2012
Last Activity: 7 June 2013, 8:42 AM EDT
Location: UK
Posts: 27
Thanks: 10
Thanked 0 Times in 0 Posts
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
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 11:29 AM.