Search code examples
arraysif-statementfor-loopawkcut

awk iterating through an array using for loop and printing based on if condition


   bash --version

bash --version GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu) Copyright (C) 2005 Free Software Foundation, Inc.

cat filename.txt|cut -d "|" -f1|tr -d [^:digit:][:punct:]
[:blank:]|tail -10|awk '{a[i++]=$0;}{k=length(a)}END{for (i=k;i>=1;i--)if (5==5){print i " "a[i]" " a[i-1]" "k}}'

10            10
9  18410      10
8 18410 18409 10
7 18409 18408 10
6 18408 18407 10
5 18407 18406 10
4 18406 18405 10
3 18405 18404 10
2 18404 18403 10
1 18403 18402 10


cat filename.txt|cut -d "|" -f1|tr -d [^:digit:][:punct:]   [:blank:]|tail -10|awk '{a[i++]=$0;}{k=length(a)}END{for (i=k;i>=1;i--)if ((a[i]-1)==a[i-1]){print i " "a[i] " "k} else print "not done"}'

not done
not done
not done
not done
not done
not done
not done
not done
not done
not done

trying to get the greatest number x if the preceding number is one lesser than x.

wc -l does not give the row count that I need. I dont need line count.

Column extracted using cut is the primary key in this csv file. File could have unknown number of blank lines at the end.

 cat filename.txt|cut -d "|" -f1|tr -d [^:digit:][:punct:]    [:blank:]|tail -10
18402
18403
18404
18405
18406
18407
18408
18409
18410

Why is this if condition evaluating to take the "else action"? (a[i]-1==a[i-1]) evaluates to false. I dont see why.


Solution

  • Given the following CSV file (filename.txt):

    1a|b|123
    2a|b|123
    3|4
    5.7|4
    66
    67
    10|11
    11|aaa
    

    we can determine the number 67 using:

    awk -f s.awk filename.txt
    

    where s.awk is:

    BEGIN {
        FS="|"
    }
    NF>=1 {
        q=$1
        gsub(/([^[:digit:]])|([[:blank:][:punct:]])*/,"",q)
        if (q==(prev+1))
            if (q>max)
                max=q
        prev=q
    }
    END {
        print "Max=" max
    }
    

    Update

    A one-liner could look like:

    awk 'BEGIN { FS="|" } { q=$1; gsub(/([^[:digit:]])|([[:blank:][:punct:]])*/,"",q); if (q==(prev+1)) {if (q>max) max=q} prev=q } END { print "Max=" max }' filename.txt