msbinu
1
I have a file which is having enrties like
entry-id 1
ABC : value
DEF :value
GHI :VALUE
entry-id 2
ABC : value
DEF :value
GHI :VALUE
entry-id 2
ABC : value
DEF :value
GHI :VALUE
and so on .. .wht i want to do is
to see if all the 3 items are present under each enrty-id and each one is havng some lavue ( not null)
is ther any simple way to do this
Thanks in adavance
binu
aigles
2
A possible solution (not a simple one) :
awk -v Items_list="ABC DEF GHI" '
BEGIN {
FS = ":" ;
Unset = 0;
Null = 1;
Valid = 2;
Invalid = 0;
Items_count = split(Items_list, Items, / /);
}
function initEntry (entry, i) {
Entry_name = entry;
for (i=1; i<=Items_count; i++)
Items_state[Items] = Unset;
}
function printEntry ( i, e_state, i_state, err, errors) {
if (! Entry_name) return;
e_state = Valid;
for (i=1; i<=Items_count; i++) {
i_state = Items_state[Items];
if (i_state != Valid) {
err = " " (i_state == Unset ? "Unset: " : "Null : ") Items;
errors = errors (e_state == Invalid ? "\n" : "") err;
e_state = Invalid;
}
}
if (e_state == Valid)
print "Ok :", Entry_name;
else {
print "Err:", Entry_name;
print errors;
}
}
/^entry-id/ {
printEntry();
initEntry($0);
next;
}
{
gsub(/[[:space:]]/, "", $1);
gsub(/[[:space:]]/, "", $2);
if ($1 && $1 in Items_state) {
Items_state[$1] = ($2 ? Valid : Null);
}
}
END {
printEntry();
}
' infile
Input data :
entry-id 1
ABC : value
DEF :value
GHI :VALUE
entry-id 2
DEF :value
GHI :VALUE
entry-id 3
ABC : value
DEF :
GHI :VALUE
entry-id 4
ABC : value
DEF :
entry-id 5
ABC : value
DEF :value
GHI :VALUE
Output :
Ok : entry-id 1
Err: entry-id 2
Unset: ABC
Err: entry-id 3
Null : DEF
Err: entry-id 4
Null : DEF
Unset: GHI
Ok : entry-id 5