How can i use awk to compare the last field to determine if the counter has increased, and need to ensure that the first 4 fields must have the same values, e.g. in the example above, the counter field has increased by 1 count for "AAA BBB CCC DDD EEE" entry.
(a) 3, because "AAA BBB CCC DDD EEE" incremented 3 times?, or
(b) 2, because "PPP QQQ RRR SSS TTT" incremented 2 times?, or
(c) 1, because "VVV WWW XXX YYY ZZZ" incremented once?, or
(d) 0, because "GGG HHH III JJJ KKK" did not increment at all?
In your example, each entry will have it's own counter:
counter (a) = 3, because "AAA BBB CCC DDD EEE" incremented 3 times
counter (b) = 2, because "PPP QQQ RRR SSS TTT" incremented 2 times
counter (c) = 1, because "VVV WWW XXX YYY ZZZ" incremented once
counter (d) = 0, because "GGG HHH III JJJ KKK" did not increment at all
And based on the counter value, some actions will be triggered.
#!/bin/ksh
nawk '
# create an variable "idx" which is a concatenation of the first 5 fields in a record/line
# as we read the file line by line
{idx=$1 FS $2 FS $3 FS $4 FS $5}
{
# see if "idx" is in array "a" - array "a" is indexed by the value of "idx"
if (idx in a)
# if "idx" is already in "a", check if the stored value (a[idx]) is less than the
# last field ($NF) of the current record/line. If it's, we see the "increase" in value
# If so, output the current line (print $0)
if (a[idx]<$NF)
print $0
# store the last field ($NF) of the current record in aarray "a" indexed by "idx"
a[idx]=$NF
}' myDiffFile | while read line
do
# read the output of "nawk" and do "doSomething" with the read line
echo "doSomething with [$line]"
done
#!/bin/ksh
nawk '
# create an variable "idx" which is a concatenation of the first 5 fields in a record/line
# as we read the file line by line
{idx=$1 FS $2 FS $3 FS $4 FS $5}
{
# see if "idx" is in array "a" - array "a" is indexed by the value of "idx"
if (!(idx in a))
print 0 ":" $0
else
# if "idx" is already in "a", check if the stored value (a[idx]) is less than the
# last field ($NF) of the current record/line. If it"s, we see the "increase" in value
# If so, output the current line (print $0)
if (a[idx]<$NF)
print 1 ":" $0
# store the last field ($NF) of the current record in aarray "a" indexed by "idx"
a[idx]=$NF
}' myDiffFile | while IFS=: read smth line
do
# read the output of "nawk" and do "doSomething" with the read line
if [ "${smth}" -eq 1 ]; then
echo "doSomething with [$line]"
else
echo "doSomethingElse with [$line]"
fi
done
strange - I get the following for the sample file you provided:
doSomethingElse with [AAA BBB CCC DDD EEE 10]
doSomething with [AAA BBB CCC DDD EEE 11]
doSomethingElse with [PPP QQQ RRR SSS TTT 5]
doSomethingElse with [VVV WWW XXX YYY ZZZ 2]
---------- Post updated at 06:29 PM ---------- Previous update was at 06:26 PM ----------
something like this?
#!/bin/ksh
nawk '
# create an variable "idx" which is a concatenation of the first 5 fields in a record/line
# as we read the file line by line
{idx=$1 FS $2 FS $3 FS $4 FS $5}
{
# see if "idx" is in array "a" - array "a" is indexed by the value of "idx"
if (!(idx in a) && FNR>1)
print 0 ":" $0
else
# if "idx" is already in "a", check if the stored value (a[idx]) is less than the
# last field ($NF) of the current record/line. If it"s, we see the "increase" in value
# If so, output the current line (print $0)
if (a[idx]<$NF || FNR==1)
print 1 ":" $0
# store the last field ($NF) of the current record in aarray "a" indexed by "idx"
a[idx]=$NF
}' myDiffFile| while IFS=: read smth line
do
# read the output of "nawk" and do "doSomething" with the read line
if [ "${smth}" -eq 1 ]; then
echo "doSomething with [$line]"
else
echo "doSomethingElse with [$line]"
fi
done
doSomethingElse with [< AAA BBB CCC DDD EEE 123]
doSomethingElse with [> PPP QQQ RRR SSS TTT 111]
doSomethingElse with [> VVV WWW XXX YYY ZZZ 333]
doSomethingElse with [> AAA BBB CCC DDD EEE 124]
Expecting:
doSomething with [> PPP QQQ RRR SSS TTT 111] # first occurrence
doSomething with [> VVV WWW XXX YYY ZZZ 333] # first occurrence
doSomething with [> AAA BBB CCC DDD EEE 124] # last counter field increased from 123 to 124
doSomething with [> PPP QQQ RRR SSS TTT 111] # first occurrence
doSomething with [> VVV WWW XXX YYY ZZZ 333] # first occurrence
doSomething with [> AAA BBB CCC DDD EEE 124] # last counter field increased from 123 to 124
doSomethingElse with [> GGG HHH III JJJ KKK 99] # last counter field decreased from 100 to 99
---------- Post updated at 01:59 PM ---------- Previous update was at 01:41 PM ----------
Updated the idx variable assignment, and we have this:
doSomethingElse with [< AAA BBB CCC DDD EEE 123]
doSomethingElse with [< GGG HHH III JJJ KKK 100]
doSomethingElse with [> PPP QQQ RRR SSS TTT 111]
doSomethingElse with [> VVV WWW XXX YYY ZZZ 333]
doSomething with [> AAA BBB CCC DDD EEE 124]
Expecting:
doSomething with [> PPP QQQ RRR SSS TTT 111] # first occurrence
doSomething with [> VVV WWW XXX YYY ZZZ 333] # first occurrence
doSomething with [> AAA BBB CCC DDD EEE 124] # last field increment
doSomethingElse with [< GGG HHH III JJJ KKK 99] # last field decrement
Thanks again for your help.
---------- Post updated at 03:51 PM ---------- Previous update was at 01:59 PM ----------
Hello, i am able to do this via regular scripting (non-awk version):