简体   繁体   中英

AWK multiple field separators | count different pattern found in field

Good afternoon,

1.AWK multiple field separators

my first question is whether having 2 field separators once located field for separator 1, how do i get inside the field that is not number one for second separator? eg:

Command

zcat  /home/technic/scripts/informe/datos/datos* \
| egrep -m1 "\|435\|t|\|0\|t"| grep ";CI[0-9]" \
| awk -F "[|;]" '{print $17}'

**record $17 (FS="|")**

2013-08-04;2013-08-10;CI6785,;HG=S;BD=S;AD=2,;NI=1,;SA,;TH,;HB=S;RG=S;PO=S;VI=S;

I want to get CI6785,

There is a very specific reason why i need to do it this way since the awk command has various filters most of them cut by "|" and in only some cases get by ";" on same awk.

2.count different pattern found in field

This case wants to have a behaviour similar to the following:

{!a["ERR|"$3"|"$15]++ || !a["IPS|"$18]++ || !a["HTTPS|"$22]++};
for(i in a) print a[i]"|"i;

--this returns the count of each different record found.

if i do this:

zcat  /home/technic/scripts/informe/datos/datos* \
| awk -F "|" --posix '!match($0, / ([0-9]{2}:){2}/);{print substr($0, RSTART, RLENGTH)}' \
| sort | uniq -c

i get the following result(part of it):

418  23:50:
392  23:51:
393  23:52:
373  23:53:
336  23:54:
321  23:55:
348  23:56:
366  23:57:
326  23:58:
341  23:59:

which basically gives me the count for every minute found, now i want to do this as first example and not have to run through a sort and uniq.

I need this to work to be able to add another clause of comparisong.

Please help most appreciated!

forgot to put whole awk to see the picture:

for DATO in {1..3}  
do       
 DATOS=$(zcat  /home/technic/scripts/informe/datos/datos"$DATO"_"$AGENCIA".tmp.gz | grep "AG|$AGENCIA" | awk -F "|" --posix 'BEGIN \
    { \
        CI=0 ; CI5 =0; CI7 =0; CI10 =0; CI20 =0; \
        H=0 ; H5 =0; H7 =0; H10 =0; H20 =0; \
        A=0 ; A5 =0; A7 =0; A10 =0; A20 =0; \
        G=0 ; G5 =0; G7 =0; G10 =0; G20 =0; \
    } \

    {!a["ERR|"$3"|"$15]++ || !a["IPS|"$18]++ || !a["HTTPS|"$22]++}; \
    /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;CI[0-9]{1,}/ {CI++} ; \
    CI > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;CI[0-9]{1,}/ && $14 < 5000 {CI5++} ; \
    CI > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;CI[0-9]{1,}/ && $14 >= 5000 && $14 < 7000 {CI7++} ; \
    CI > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;CI[0-9]{1,}/ && $14 >=7000 && $14 < 10000 {CI10++} ; \
    CI > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;CI[0-9]{1,}/ && $14 >=10000&& $14 < 20000 {CI20++} ; \

    /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;H[0-9]{1,}/ {H++} \
    H > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;H[0-9]{1,}/ && $14 < 5000 {H5++} ; \
    H > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;H[0-9]{1,}/ && $14 >= 5000 && $14 < 7000 {H7++} ; \
    H > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;H[0-9]{1,}/ && $14 >=7000 && $14 < 10000 {H10++} ; \
    H > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;H[0-9]{1,}/ && $14 >=10000&& $14 < 20000 {H20++} ; \

    /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;A[0-9]{1,}/ {A++} \
    A > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;A[0-9]{1,}/ && $14 < 5000 {A5++} ; \
    A > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;A[0-9]{1,}/ && $14 >= 5000 && $14 < 7000 {A7++} ; \
    A > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;A[0-9]{1,}/ && $14 >=7000 && $14 < 10000 {A10++} ; \
    A > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;A[0-9]{1,}/ && $14 >=10000&& $14 < 20000 {A20++} ; \

    /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;G[0-9]{1,}/ {G++} \
    G > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;G[0-9]{1,}/ && $14 < 5000 {G5++} ; \
    G > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;G[0-9]{1,}/ && $14 >= 5000 && $14 < 7000 {G7++} ; \
    G > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;G[0-9]{1,}/ && $14 >=7000 && $14 < 10000 {G10++} ; \
    G > 0 && /\|GetAvail/ && /\|435\|t|\|0\|t/ && $17 ~ /;G[0-9]{1,}/ && $14 >=10000&& $14 < 20000 {G20++} ; \
    END {
    if (CI > 0){ print "DISPOCI|"CI"|"CI5"|"(CI5*100/CI)"|"CI7"|"(CI7*100/CI)"|"CI10"|"(CI10*100/CI)"|"CI20"|"(CI20*100/CI); } \
    if (H > 0){ print "DISPOH|"H"|"H5"|"(H5*100/H)"|"H7"|"(H7*100/H)"|"H10"|"(H10*100/H)"|"H20"|"(H20*100/H); } \
    if (A > 0){ print "DISPOA|"A"|"A5"|"(A5*100/A)"|"A7"|"(A7*100/A)"|"A10"|"(A10*100/A)"|"A20"|"(A20*100/A); } \
    if (G > 0){ print "DISPOG|"G"|"G5"|"(G5*100/G)"|"G7"|"(G7*100/G)"|"G10"|"(G10*100/G)"|"G20"|"(G20*100/G); } \
    for(i in a) print a[i]"|"i; \
    }';)" "
    echo ${DATOS[@]} | sed "s/[ ][ ]*/\n/g" | grep 'DISPO'
    echo "todos los datos en array de dispos ++++++++++++++++++++++"
for REC in $(echo ${DATOS[@]} | sed "s/[ ][ ]*/\n/g" | grep 'DISPO')
do
    echo $REC | awk -F "|" '{print $1}'
    if [ -z ${DISPO[$(echo $REC | awk -F "|" '{print $1}')]} ];then
        echo "no ha encontrado añado registro ++++++++++++++++++++++++"
        DISPO[$(echo $REC | awk -F "|" '{print $1}')]=$(echo $REC | awk -F "|" '{print $2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"$9"|"$10}')
        echo ${DISPO[$(echo $REC | awk -F "|" '{print $1}')]}"<----------------- valor dentro de array"
    else
       echo "na encontrado sumo registro ++++++++++++++++++++++++"
       PARAM1=$(($(echo ${DISPO[$(echo $REC | awk -F "|" '{print $1}')]} | awk -F "|" '{print $1}') + $(echo $REC | awk -F "|" '{print $2}'))); echo "$PARAM1<------------parametro 1"
       PARAM2=$(($(echo ${DISPO[$(echo $REC | awk -F "|" '{print $1}')]} | awk -F "|" '{print $2}') + $(echo $REC | awk -F "|" '{print $3}'))); echo "$PARAM2<------------parametro 2"
       PARAM3=$(($(echo ${DISPO[$(echo $REC | awk -F "|" '{print $1}')]} | awk -F "|" '{print $4}') + $(echo $REC | awk -F "|" '{print 5}'))); echo "$PARAM3<------------parametro 3"
       PARAM4=$(($(echo ${DISPO[$(echo $REC | awk -F "|" '{print $1}')]} | awk -F "|" '{print $6}') + $(echo $REC | awk -F "|" '{print $7}'))); echo "$PARAM4<------------parametro 4"
       PARAM5=$(($(echo ${DISPO[$(echo $REC | awk -F "|" '{print $1}')]} | awk -F "|" '{print $8}') + $(echo $REC | awk -F "|" '{print $9}'))); echo "$PARAM5<------------parametro 5"
       PORC1=$(echo "scale=2;($PARAM2*100)/$PARAM1" | bc);PORC2=$(echo "scale=2;($PARAM3*100)/$PARAM1" | bc);PORC3=$(echo "scale=2;($PARAM4*100)/$PARAM1" | bc);PORC4=$(echo "scale=2;($PARAM5*100)/$PARAM1" | bc)
       DISPO[$(echo $REC | awk -F "|" '{print $1}')]="$PARAM1|$PARAM2|$PORC1|$PARAM3|$PORC2|$PARAM4|$PORC3|$PARAM5|$PORC4"                          
    fi
done
for REC in $(echo ${DATOS[@]} | sed "s/[ ][ ]*/\n/g" | grep 'ERR|')
do
    echo "$REC"
    echo "------- ERR -------"
done
for REC in $(echo ${DATOS[@]} | sed "s/[ ][ ]*/\n/g" | grep 'IPS|')
do
    echo "$REC"
    echo "------- IPS -------"
done    
for REC in $(echo ${DATOS[@]} | sed "s/[ ][ ]*/\n/g" | grep 'HTTPS|')
do
    echo "$REC"
    echo "------- HTTPS -------"
done         
done

o_0budaya0_o

尝试将字段拆分为一个数组:

... | awk -F"|" '{split($17,a,";"); print a[3]}'

The split a field into subfields, use awk's split function:

To print the third subfield from the second main field, like 4 in 1|2;3;4|5 :

echo '1|2;3;4|5' | awk -F'|' '{ split($2, array, /;/); print array[3]; }'

Good afternoon,

Thank you so much for your support it somehow helped a lot i finally ended up with the solution which was based on your help and here it goes the solution for anyone with curiosity for it:

cat /xmllogs/logs/xml/stats.log | awk -F "|" '/GetAvail/ && /AG\\|3857928/ {split($17,b,";"); !a[b[1]]++}; END {for(i in a) print a[i]"|"i;}'

This allows you to obtain counting for different value found for a pattern given.

Cheers thx! take cares!

o_0budaya0_o

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM