Tried both solutions but did not work. Not sure if I am missing anything.
just fyi I have very limited knowledge of awk/shell scripting so something may be obvious but I may still miss it.
$ cat sortme
function sortme(x,f, j, k)
{
for (j=1;j<f;j++)
for (k=1;k<=f-j;k++)
if (x[k] > x[k+1]) {
t=x[k]
x[k]=x[k+1]
x[k+1]=t
}
}
BEGIN {FS = ":" }
{
pref=$1
m = split(pref, srt_pref, ",")
n = split(baseline, srt_baseline, ",")
if (sortme(srt_pref, m) == sortme(srt_baseline, n)) svcstatus = "OK"
else svcstatus = "NOT-OK"
printf "%s %s %s %s\n", sname, pref, baseline, svcstatus
}
$ snamelist="AAAB,ABCddf,LMNS,PQR:VALUE1 AAB,ABSSf,LMNS,DFD:VALUE2 LMNS,ABCddf,PQR,AAAB:VALUE3"
$ for sname in $snamelist
> do
> BASELINE="AAAB,ABCddf,LMNS,PQR"
> echo "$sname" | awk -v baseline="$BASELINE" -f sortme
> done
AAAB,ABCddf,LMNS,PQR AAAB,ABCddf,LMNS,PQR OK
AAB,ABSSf,LMNS,DFD AAAB,ABCddf,LMNS,PQR OK
LMNS,ABCddf,PQR,AAAB AAAB,ABCddf,LMNS,PQR OK
$
Output is not the expected output. 1st line should be OK, 2nd line NOT-OK and 3rd line OK
I also tried bubble sort method but its erroring out and not able to understand the error.
$ snamelist="AAAB,ABCddf,LMNS,PQR:VALUE1 AAB,ABSSf,LMNS,DFD:VALUE2 LMNS,ABCddf,PQR,AAAB:VALUE3"
$ BASELINE="AAAB,ABCddf,LMNS,PQR"
$ printf "%s\n" $snamelist |
> awk -F: -v baseline="$BASELINE" '
> function arrsort(A, n,i,c){
> n=length(A)
> do {
> c="";
> for(i=1;i<n;i++) {
> if(A>A[i+1]) {
> c=A; A=A[i+1]; A[i+1]=c
> }
> }
> }
> while(c!="")
> }
>
> BEGIN{
> split(baseline,B,/,/)
> arrsort(B)
> }
>
> {
> split($1,F,/,/)
> arrsort(F)
> svcstatus="OK"
> for(i in F) if(B!=F) svcstatus="NOT-OK"
> printf "%s %s %s %s\n", $0, $1, baseline, svcstatus
> }
> '
awk: Cannot read the value of B. It is an array name.
The source line number is 3.
$
If there is any other solution to find if both strings have same values, I am ok to use it as long as its in awk and I am be able to compare and print output as below
echo "$sname" | awk -v baseline="$BASELINE" 'BEGIN { FS=":" }
{<DEFINE SORTME FUNCTION HERE>}
{ pref=$1 ; if (sortme(pref) == sortme(baseline)) { svcstatus = "OK" } else { svcstatus = "NOT-OK" } ;
printf "%-35s %-35s %-35s %-10s\n", sname, pref, baseline, svcstatus}'
Thanks for your responses.