As I've mentioned in the comments bellow the question, I've found a somewhat simple awk
solution: concatenate the two numberals to create one long string, replace all dots with space ( to allow using space as default field separator in awk), and go through the string comparing field with file+half.
Basic command
printf ${a[1]}${a[2]} | awk '{gsub("\\."," "); half=NF/2}; { for ( x=1; x<=half; x++ ) { if ( $x==$(x + half) ) printf "."$x };}'
I've tested this with gawk and mawk, worked in both.
Here's output with first example ( .1.3.6.1.4.1.232.13600256 and .1.3.6.1.4.1.232.13600276 ) :
$ printf ${a[1]}${a[2]} | awk '{gsub("\\."," "); half=NF/2}; { for ( x=1; x<=half; x++ ) { if ( $x==$(x + half) ) printf "."$x };}'
.1.3.6.1.4.1.232
Multiple comparisons
If you want to compare multiple strings at the same time, concatinate them together and separate with newline in printf, then add printf at the end of awk command like so:
printf "${a[1]}${a[2]}\n${a[3]}${a[4]}" | awk '{gsub("\\."," "); half=NF/2}; { for ( x=1; x<=half; x++ ) { if ( $x==$(x + half) ) printf "."$x }; printf "\n"}'
Output:
$ printf "${a[1]}${a[2]}\n${a[3]}${a[4]}" | awk '{gsub("\\."," "); half=NF/2}; { for ( x=1; x<=half; x++ ) { if ( $x==$(x + half) ) printf "."$x }; printf "\n"}'
.1.3.6.1.4.1.232 # same for a[1] and a[2]
.1.3.6.1.4.1.759 # same for a[3] and a[4]
Limiting the output
Now, kos's comment appropriately noticed that OP wants only 7 numbers to be displayed. For that purpose you can add pipe to the cut -d'.' -f1-8
command. Like so:
printf "${a[5]}${a[6]}" | mawk '{gsub("\\."," "); half=NF/2}; { for ( x=1; x<=half; x++ ) { if ( $x==$(x + half) ) printf "."$x }; printf "\n"}' | cut -d'.' -f1-8
Here's sample output from my terminal:
$ a[5]=.1.3.6.1.4.1.232.13600256.885
$ a[6]=.1.3.6.1.4.1.232.13600256.885
$ printf "${a[5]}${a[6]}" | mawk '{gsub("\\."," "); half=NF/2}; { for ( x=1; x<=half; x++ ) { if ( $x==$(x + half) ) printf "."$x }; printf "\n"}' | cut -d'.' -f1-8
.1.3.6.1.4.1.232.13600256.885
half) ) printf "."$x }; printf "\n"}' | cut -d'.' -f1-8 <
.1.3.6.1.4.1.232
Simplifying even further
Again, everything can be put in an awk script
#!/usr/bin/awk -f
{
gsub("\\."," ");
half=NF/2
};
{
for ( x=1; x<=half; x++ ) {
if ( $x==$(x + half) ) printf "."$x
};
printf "\n"
}
Sample run:
$ printf "${a[5]}${a[6]}" | num-comp.awk | cut -d'.' -f1-8
.1.3.6.1.4.1.232
Comparison up to the first not-equal number
Awk has a very useful function substr(string,X,Y)
which allows cutting or "cropping" a string, from first character (x) to the ending (Y). So knowing that, let's take the two numbers as two fields of one string, and run them through while loop. We're gonna keep increasing the substring length (start to ending) until they're no longer equal. Once we encounter the unequal substrings, we quit, and print the last known equal substring.
echo ".1.3.6.1.4.1.232.13600256\t.1.3.6.1.4.1.232.13600276" | awk 'BEGIN{i=1}{ while(substr($1,1,i)==substr($2,1,i)){var=substr($1,1,i);i++};} END{print var}'
Special thanks to terdon for suggesting use of substr function, which I previously didn't know even existed
a
is my array name and 1,2 are index positions in them where these numerals are stored${a[1]}=".1.3.6.1.4.1.232.13600256.885"
andb="${.1.3.6.1.4.1.232.13600256.885}"
you want.1.3.6.1.4.1.232
and not.1.3.6.1.4.1.232.885
, correct?