Awk 用awk转置行和列

#!/usr/bin/awk

NR == 1 {
  for ( i = 1 ; i <= NF ; i++ ){
    line[i] = $i;
  }
  
  next;
}

{
  for ( i = 1 ; i <= NF ; i++ ){
    line[i] = line[i] OFS $i;
  }
}

END {
  for ( i = 1 ; i <= length(line) ; i++){
    print line [i]
  }
}

Awk rot13x()

function rot13x(str,    from, to, x, y, z, letter, char, buf) {

# rot13x for awk - Topcat Software LLC. 2010
# http://www.topcat.hypermart.net/index.html
# rot13 cipher extended to include digits
#
# more info about rot13 at: http://en.wikipedia.org/wiki/ROT13
# a modification of the example found at:
# http://www.miranda.org/~jkominek/rot13/awk/
#
# example:
#
# foo = rot13x("some string')
# bar = rot13x(foo)
# print foo
# print bar

    from = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    to   = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm5678901234"
    x    = 62 # length from | to
    y    = length(str)

    for (z = 1; z <= x; z++) {
         letter[substr(from, z, 1)] = substr(to, z, 1)
    }

    for (z = 1; z <= y; z++) {
        char = substr(str, z, 1)
        buf  = (char in letter) ? buf letter[char] : buf char
    }

    return buf

}

Awk 与Awk合并线条

REGEX="/^[^|]/"
func_merge_lines "$REGEX" $TMP1 $TMP2
 
function func_merge_lines
{
     awk --re-interval "NR==1 \
               { s=\$0;next } \
          "$1"{ s=s\$0;next } \
     { print s;s=\$0 } END {if(s)print s}" $2 > $3 # merge lines. 
                                                   # Looks for lines not starting with pipe
                                                   # then merges line below to end.
}

Awk 按大小排序目录(人类可读)

du --max-depth 1|sort -n|awk '{ $1=$1/1024; printf "%.2f MB: %s\n",$1,$2 }'