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
cutandgrepwhenbashitself 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.com1.1.1.1but also a group likeaes-LANTRONIX, those are two different types of object, how can they be handled the same?$,|) in quotes, they lose their special meaning and are treated as plain literal characters<<< "$(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.