0

This script, even though it works, it takes too much longer to execute in large files, due to the while loops, but I could not think of any faster way to go thru the files. Any suggestions to improve its speed if welcome.

I have a list like this. This is how the objFile input looks like:

 object-group network AES_PCH_CONGONHAL_CCEEP network-object host 1.1.1.1 object-group network aes-LANTRONIX description LANTRONIX network-object object AES_PCH_CONGONHAL_CCEEP network-object object AES_PCH_CONGONHAL_CCEER 

This is the IP looks like:

1.1.1.1 aes-LANTRONIX 

I need to search for a IP list and bring back all 'object-group or 'object' the IP is in and the full content of each object. For instance, IP 1.1.1.1. The output should look like this

object-group network AES_PCH_CONGONHAL_CCEEP network-object host 1.1.1.1 

This could be recursive, having a object inside a object, that`s why a have an array for further looking

function findVar { while read ip do local flag=0 while read objFile do if ! echo $objFile |grep -q -e '^object-group\|^object' then local allip+=" $objFile\n" if echo $objFile | grep -q -e "$ip$\|$ip\ " then local objectNAME=$(echo $object | cut -d' ' -f3) grep access-list $showrunn | grep -e "$objectNAME\|$ip " >> $acl tempV+=("$objectNAME") flag=1 fi else if [ $flag -eq 1 ] then echo $object echo -e "$allip" unset allip flag=0 else unset allip fi object=$(echo $objFile |grep -e '^object-group\|^object') fi done <<< "$(cat $1)" done <<< "$(cat $2)" } num_words=${#tempV[@]} for i in "${!tempV[@]}"; do findVar "$objBKP" "$var" done 
8
  • 2
    You're using a lot of external programs like cut and grep when bash itself has builtin tools to do the same thing. Not really a performance issue, but you're using <<< when a simple input redirection would work: while ...; do ... done < "$1". Given that your primary problem is that your code is too slow, I think this is a better fit for codereview.stackexchange.com Commented Mar 11, 2016 at 21:31
  • You say (I think) that you could be looking for a host like 1.1.1.1 but also a group like aes-LANTRONIX, those are two different types of object, how can they be handled the same? Commented Mar 11, 2016 at 21:59
  • 1
    If you want speed, bash is the wrong tool. I'd choose perl. If you want a fast POSIX-based shell, use ksh93 Commented Mar 11, 2016 at 22:01
  • You have to be careful with regular expressions in bash. When you put regex metachars ($, |) in quotes, they lose their special meaning and are treated as plain literal characters Commented Mar 11, 2016 at 22:02
  • 1
    <<< "$(cat $1)" as @chepner pointed out. But I feel that could be a performance issue in that it will read the entire file into memory, rather than a line at a time. Use < "$1" instead. Many of the external programs are unnecessary. Commented Mar 11, 2016 at 22:52

1 Answer 1

1

gawk to the rescue!

a prototype solution for the given structure can be

$ gawk -v RS="object-group" '/1\.1\.1\.1/{print RT $0}' file object-group network AES_PCH_CONGONHAL_CCEEP network-object host 1.1.1.1 

I'm not sure I understand your nested structure and what you expect as output in that case.

Note that you need multi-char RS, which is not available in all awks.

Sign up to request clarification or add additional context in comments.

2 Comments

Note you'd want to make RS "object-group network"; this is a Cisco config file and there are a few different types of object groups.
yes, probably many other things as well, just wanted to add gawk as an alternative to heavy bash scripting.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.