awk '/hello/{print $0}'
awk -f script1.awk
awk 'BEGIN { print "hello" }'
awk 'BEGIN { print "John\47s cat"} '
awk 'BEGIN { print "John'\''s cat"} '
awk -v char="'" 'BEGIN { print "John"char"s cat"} '
awk '/awk/ { print $0 }' $0
awk '/awk/' $0
awk 'length($0)>50' $0
awk '{ if (length($0) > 50) print }' $0
awk ' { if (length($0) > length(max)) max=$0 } END { print max" "length(max) }' $0
awk 'NF > 10' $0
awk 'NF > 0 { print NR,FNR,NF }' $0
awk 'BEGIN { OFS="->"} { print $1,$2,$3,$4 }' $0
awk '!/# awk/&&(NF>1) { print $0 }' $0
awk 'BEGIN { ORS="|\n"} { print $0 }' $0
awk '{ print "Row: ",$0 }' $0 -
awk '{ gsub("awk","gawk"); print $0}' $0
awk -f script1.awk $0
gawk -lordchr 'BEGIN { print chr(65) }'
awk '{ print substr($0,1,20) "|" substr($0,21) }' $0
awk '{ print index($0,"BEGIN"),$0 }' $0
on right of "." using " " as separator
awk '{ FS="."; first=$1; $1=""; split($0,a," "); print first,a[2] }' $0
awk '($2~/awk/) && ($0!~/gawk/) {print $0}' $0
awk 'tolower($0)~/begin/' $0
awk -v IGNORECASE=1 '/begin/' $0
awk 'BEGIN{IGNORECASE=1 } /begin/' $0
awk '/^# [0-9]{0,1}[13579]\./' $0
awk '/^# [0-9]{0,1}[^13579]\./' $0
awk '/^# [0-9]*[^13579]\./' $0
awk '/^# [0-9]+[13579]\./' $0
awk '/^# [0-9]?\./' $0
awk 'BEGIN{RE="[0-9]"} (NF>0 && $0!~RE)' $0
awk '{ gsub(/{/,"{\n\t"); gsub(/}/,"\n}\n"); print }' $0
awk -v RS="." // $0
awk '/^# [0-9]+\./ {
print $0
$1=$2=""
split($0,vett," ")
asort(vett)
for (i in vett)
print i ": " vett[i]
print "WORDS COUNT: " length(vett) "\n"
}' $0
echo "12345ABCDE1234567890ABC"|awk 'BEGIN{FIELDWIDTHS="5 5 10 3"} {
print \
"Initial String:",$0,"\n"\
"Using FIELDWIDTHS to extract Fixed Width Fields","\n"\
"First 5 Numbers:",$1,"\n"\
"First 5 Chars:",$2,"\n"\
"First 10 Numbers:",$3,"\n"\
"First 3 Chars:",$4,"\n"\
}'
awk '{
while (getline temp) {
print "$0:",$0," getline:",temp
}
}' $0
awk -v FILENAME=$0 '{ \
getline temp < FILENAME; \
print $0 " | " temp \
}' $0
awk '{ \
print $1 > "/tmp/1"
print $2 > "/tmp/2"
} END { \
print "First column"
system("cat /tmp/1")
print "Second column"
system("cat /tmp/2")
}' $0
awk '/{/,/}/' $0
awk 'NF > 3 {next} // {print "NF:",NF,$0}' $0
awk 'NR > 10 { exit } // { print "NR:",NR,$0}' $0
awk 'BEGIN {
for (i in ARGV)
print i":",ARGV[i];
print "ARGC:",ARGC
}' $0
awk ' { print FILENAME; exit }' $0
awk 'BEGIN {
for(i in PROCINFO) {
if (isarray(PROCINFO[i]))
for(j in PROCINFO[i])
print i,j,PROCINFO[i][j]
else
print i,PROCINFO[i]
}
}' $0
43. match example using on filter, printing and showing return, from a variable containing regular expression
awk '
BEGIN {
REG=" ..\\."
}
match($0,REG) > 0 {
print("match($0,REG):",match($0,REG),$0)
}
' $0
awk '$2 ~ /..\./ {
num = $2
gsub(/\./,"",num)
n = strtonum(num);
print "strtonum("num"*10) = " n*10," ",$0
}' $0
awk 'BEGIN {
print "systime():",systime()
print "mktime(\"2000 01 01 00 00 00\"):",mktime("2000 01 01 00 00 00")
print "strftime(\"%d %m %Y %H %M %S\",946681200):",strftime("%d %m %Y %H %M %S",946681200)
}' $0
awk '
function toNumAndQuadratic(s) {
return strtonum(s)^2
}
BEGIN {
NumExpr="^# [0-9]+\\."
}
match($0,NumExpr) > 0 {
print "toNumAndQuadratic(\""$2"\"): " toNumAndQuadratic($2)," " $0
}
' $0
awk '
function odd() {
return "called function odd";
}
function even() {
return "called function even";
}
BEGIN {
NumExpr="^# [0-9]+\\."
}
match($0,NumExpr) > 0 {
num = strtonum($2)
t = num % 2 != 0 ? "odd" : "even"
print "num:",num ",","type:",t ",",@t()
}
' $0
awk '
BEGIN {
NumExpr="^# [0-9]+\\."
}
match($0,NumExpr) > 0 {
print "ORIGINAL LINE READ: " $0;
split($0,a," ");
b = ""
for (i in a)
b = b a[i] " "
print "AFTER SPLIT / JOIN: " b,"\n"
}
' $0
awk '
BEGIN {
print "# Very Simple AWK Examples"
}
/#!\/bin\/bash/ { next }
/^# [0-9]*\./ {
if (NOT_FIRST)
print "```"
else
NOT_FIRST = 1
$1 = "##"
print
print "```"
next
}
{
sub(/^# /,"",$0)
print
}
END {
print "```"
}
' $0