├── Chapter01 ├── f1.awk ├── f2.awk ├── inc1 ├── cmd.awk ├── inc2 ├── inc3 ├── emp.awk ├── while1.awk ├── validate.awk ├── header.awk ├── displayargs.awk ├── empinfo.txt ├── emp_comment.awk ├── seq.awk ├── cars.dat └── emp.dat ├── Chapter10 ├── list.txt ├── matrix.txt ├── label.dat ├── sample.txt ├── table.txt ├── marks.txt ├── transpose_matrix.awk ├── transpose2.awk ├── dec2bin.awk ├── emp2.dat ├── cars.dat ├── array2html.awk ├── emp.dat └── http_report.awk ├── Chapter06 ├── sample.txt ├── ternary.awk ├── post-decrement.awk ├── post-increment.awk ├── pre-decrement.awk ├── pre-increment.awk ├── reassign.awk ├── count_login_users.awk ├── emptrystring_check.awk ├── octalcheck.awk ├── assign.awk ├── maxuid.awk ├── str_fname_concatenate.awk ├── whitespacecheck.awk ├── marks.txt ├── string.awk ├── sum.awk ├── average.awk ├── cars.dat └── emp.dat ├── Chapter07 ├── sample.txt ├── break2.awk ├── for1.awk ├── label.dat ├── while1.awk ├── check_zero.awk ├── dowhile1.awk ├── car_old_new.awk ├── check_even_odd.awk ├── continue1.awk ├── exit2.awk ├── for3.awk ├── break1.awk ├── next.awk ├── continue2.awk ├── if_else_if1.awk ├── marks.txt ├── while3.awk ├── ifelse1.awk ├── dowhile2.awk ├── if_else_if2.awk ├── break3.awk ├── exit1.awk ├── switch1.awk ├── switch2.awk ├── for2.awk ├── while2.awk ├── dowhile3.awk ├── cars.dat └── emp.dat ├── Chapter09 ├── sample.txt ├── nondecimal.awk ├── tcpserver.awk ├── tcpclient.awk ├── small2capital.awk ├── 2wayio.awk ├── udpserver.awk ├── marks.txt ├── udpclient.awk ├── view_webpage.awk ├── asort_arr.awk ├── asorti_1.awk ├── calc.awk ├── cars.dat ├── asorti_2.awk ├── asort_newarr.awk ├── num.txt ├── emp.dat ├── codeprof.awk ├── user_functions.awk ├── awkprof.out └── codeprof.prof ├── Chapter05 ├── printf_pipe.awk ├── label.dat ├── output_separator.awk ├── printf_append.awk ├── names.sorted ├── printf_symbol.awk ├── printf_leading_zero.awk ├── printf_positional_modifier.awk ├── printf_example.awk ├── printf_hash_modifier.awk ├── close.awk ├── printf_hex.awk ├── printf_width.awk ├── printf_precision.awk ├── printf_redirection1.awk ├── cars.dat ├── emp.dat └── emp_report ├── Chapter08 ├── match2.awk ├── cosfunc.awk ├── srand1.awk ├── sinefunc.awk ├── getline_var.awk ├── getline_file.awk ├── logfunc.awk ├── expfunc.awk ├── getline_user.awk ├── or.awk ├── and.awk ├── getline_var_file.awk ├── gsub1.awk ├── rand1.awk ├── sqrtfunc.awk ├── strftime.awk ├── sub2.awk ├── xor.awk ├── fflush.awk ├── getline_user_var.awk ├── lshift.awk ├── rshift.awk ├── mktime.awk ├── sprintf.awk ├── gensub2.awk ├── func4.awk ├── getline_pipe2.awk ├── getline_var_pipe.awk ├── gensub3.awk ├── marks.txt ├── close1.awk ├── gensub1.awk ├── getline_var_coprocess.awk ├── tolower1.awk ├── toupper1.awk ├── strtonum.awk ├── length1.awk ├── intfunc.awk ├── length2.awk ├── rand2.awk ├── split1.awk ├── sub1.awk ├── split2.awk ├── test.awk ├── match1.awk ├── srand2.awk ├── func2.awk ├── systime.awk ├── system.awk ├── index.awk ├── strftime1.awk ├── getline_pipe.awk ├── substr1.awk ├── cars.dat ├── func1.awk ├── func3.awk └── emp.dat ├── Chapter03 ├── str2num.awk ├── setofs.awk ├── environ.awk ├── label.dat ├── read_line_after_5.awk ├── argind.awk ├── nf1.awk ├── fs2.awk ├── ignorecase.awk ├── ors.awk ├── arguments.awk ├── phonelist.awk ├── rs1.awk ├── strings.awk ├── fs1.awk ├── nf2.awk ├── subsep.awk ├── mod_subsep.awk ├── numeric.awk ├── erro.awk ├── fnr.awk ├── txt2csv.awk ├── match.awk ├── fnr2.awk ├── argc_argv.awk ├── filename.awk ├── varying_fields.dat ├── procinfo.awk ├── fieldwidth.awk ├── ofmt.awk ├── 2convfmt.awk ├── convfmt.awk ├── cars.dat ├── emp.dat └── table ├── Chapter04 ├── arr_delete_err.awk ├── arr_forloop2.awk ├── arr_basic_split.awk ├── basic_array.awk ├── arr_var_assign.awk ├── arr_using_split.awk ├── check_arry.awk ├── marks.txt ├── arr_delete.awk ├── multi_arr1.awk ├── multi_arr2.awk ├── arr_del_forloop.awk ├── arr_delall.awk ├── arr_forloop.awk ├── multi_arr3.awk ├── arr_access.awk ├── arr_member_check.awk ├── multi_arr4.awk ├── cars.dat ├── marks_summary.awk └── emp.dat ├── Chapter02 ├── backreference.txt ├── interval_regex.txt ├── gopr.txt └── dot_regex.txt ├── LICENSE └── README.md /Chapter01/f1.awk: -------------------------------------------------------------------------------- 1 | NR==2 { print NR, $0 } 2 | -------------------------------------------------------------------------------- /Chapter01/f2.awk: -------------------------------------------------------------------------------- 1 | NR==4 { print NR, $0 } 2 | -------------------------------------------------------------------------------- /Chapter10/list.txt: -------------------------------------------------------------------------------- 1 | 10 2 | 20 3 | 15 4 | 40 5 | -------------------------------------------------------------------------------- /Chapter01/inc1: -------------------------------------------------------------------------------- 1 | BEGIN { print "This is inc1." } 2 | -------------------------------------------------------------------------------- /Chapter06/sample.txt: -------------------------------------------------------------------------------- 1 | this is string concatenation example 2 | -------------------------------------------------------------------------------- /Chapter07/sample.txt: -------------------------------------------------------------------------------- 1 | this is string concatenation example 2 | -------------------------------------------------------------------------------- /Chapter09/sample.txt: -------------------------------------------------------------------------------- 1 | this is string concatenation example 2 | -------------------------------------------------------------------------------- /Chapter01/cmd.awk: -------------------------------------------------------------------------------- 1 | BEGIN { print "***Emp Info***" } 2 | { print } 3 | -------------------------------------------------------------------------------- /Chapter01/inc2: -------------------------------------------------------------------------------- 1 | @include "inc1" 2 | BEGIN { print "This is inc2." } 3 | -------------------------------------------------------------------------------- /Chapter01/inc3: -------------------------------------------------------------------------------- 1 | @include "inc2" 2 | BEGIN { print "This is inc3." } 3 | -------------------------------------------------------------------------------- /Chapter05/printf_pipe.awk: -------------------------------------------------------------------------------- 1 | { 2 | print $1 | sort -r > "fname_sorted" 3 | } 4 | -------------------------------------------------------------------------------- /Chapter08/match2.awk: -------------------------------------------------------------------------------- 1 | { 2 | if (match( $0, /maruti/ )) 3 | print 4 | } 5 | -------------------------------------------------------------------------------- /Chapter03/str2num.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | a=4; b=5; 3 | print ( a b ) + 4 ; 4 | } 5 | -------------------------------------------------------------------------------- /Chapter07/break2.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | while (1) 3 | print "infinite loop" 4 | } 5 | -------------------------------------------------------------------------------- /Chapter07/for1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | for ( i = 1; i <=10; i++ ) 3 | print i 4 | } 5 | 6 | -------------------------------------------------------------------------------- /Chapter10/matrix.txt: -------------------------------------------------------------------------------- 1 | a1;a2;a3;a4 2 | b1;b2;b3;b4 3 | c1;c2;c3;c4 4 | d1;d2;d3;d4 5 | -------------------------------------------------------------------------------- /Chapter01/emp.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | BEGIN { print "***Emp Info***" } 4 | { print } 5 | -------------------------------------------------------------------------------- /Chapter01/while1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { n=1 2 | while ( n < 10) 3 | { 4 | print n; 5 | n++ 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Chapter03/setofs.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | BEGIN { OFS = "\t:" } 4 | {print $1,$3,$4} 5 | 6 | -------------------------------------------------------------------------------- /Chapter03/environ.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | OFS="=" 3 | for( v in ENVIRON ) 4 | print v, ENVIRON[v]; 5 | } 6 | -------------------------------------------------------------------------------- /Chapter03/label.dat: -------------------------------------------------------------------------------- 1 | Jack 2 | 9988776655 3 | jack@gmail.com 4 | 5 | Sam 6 | 8855991122 7 | sam@gmail.com 8 | -------------------------------------------------------------------------------- /Chapter05/label.dat: -------------------------------------------------------------------------------- 1 | Jack 2 | 9988776655 3 | jack@gmail.com 4 | 5 | Sam 6 | 8855991122 7 | sam@gmail.com 8 | -------------------------------------------------------------------------------- /Chapter06/ternary.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | p = 10; q=20 3 | ( p > q )? max=p: max =q 4 | print max 5 | } 6 | 7 | -------------------------------------------------------------------------------- /Chapter07/label.dat: -------------------------------------------------------------------------------- 1 | Jack 2 | 9988776655 3 | jack@gmail.com 4 | 5 | Sam 6 | 8855991122 7 | sam@gmail.com 8 | -------------------------------------------------------------------------------- /Chapter07/while1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | i = 1 3 | while ( i <=10 ) 4 | { 5 | print i 6 | i++ 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Chapter10/label.dat: -------------------------------------------------------------------------------- 1 | Jack 2 | 9988776655 3 | jack@gmail.com 4 | 5 | Sam 6 | 8855991122 7 | sam@gmail.com 8 | -------------------------------------------------------------------------------- /Chapter03/read_line_after_5.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | { 4 | if ( NR > 5 ) 5 | { print NR, $0 } 6 | } 7 | -------------------------------------------------------------------------------- /Chapter05/output_separator.awk: -------------------------------------------------------------------------------- 1 | BEGIN { OFS = ":" 2 | ORS = "\n\n" 3 | } 4 | { print $1,$2,$3,$4,$5 } 5 | 6 | -------------------------------------------------------------------------------- /Chapter06/post-decrement.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | p = 5; 3 | q = p--; 4 | printf "p = %d, q = %d\n", p,q 5 | } 6 | -------------------------------------------------------------------------------- /Chapter06/post-increment.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | p = 5; 3 | q = p++; 4 | printf "p = %d, q = %d\n", p,q 5 | } 6 | -------------------------------------------------------------------------------- /Chapter06/pre-decrement.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | p = 5; 3 | q = --p; 4 | printf "p = %d, q = %d\n", p,q 5 | } 6 | -------------------------------------------------------------------------------- /Chapter06/pre-increment.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | p = 5; 3 | q = ++p; 4 | printf "p = %d, q = %d\n", p,q 5 | } 6 | -------------------------------------------------------------------------------- /Chapter06/reassign.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | demo = 10; 3 | print demo; 4 | demo = "Hello"; 5 | print demo; 6 | } 7 | -------------------------------------------------------------------------------- /Chapter08/cosfunc.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "cos(90) = ", cos(90); 3 | print "cos(45) = ", cos(45); 4 | } 5 | 6 | -------------------------------------------------------------------------------- /Chapter08/srand1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "Random num1 = ", srand() 3 | print "Random num2 = ", srand() 4 | } 5 | -------------------------------------------------------------------------------- /Chapter03/argind.awk: -------------------------------------------------------------------------------- 1 | END { 2 | print "ARGIND : ", ARGIND; 3 | print "Current Filename : ", ARGV[ARGIND]; 4 | } 5 | -------------------------------------------------------------------------------- /Chapter03/nf1.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | { print "No. of fields : ", NF, 4 | " and last field :",$(NF) } 5 | -------------------------------------------------------------------------------- /Chapter06/count_login_users.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | FS=":" 3 | } 4 | $NF ~ /bash/{ n++ } 5 | END { 6 | print n 7 | } 8 | -------------------------------------------------------------------------------- /Chapter08/sinefunc.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "sin(90) = ", sin(90); 3 | print "sin(45) = ", sin(45); 4 | } 5 | 6 | -------------------------------------------------------------------------------- /Chapter04/arr_delete_err.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | arr[10] = "audi"; 3 | delete arr; 4 | arr = "bmw"; 5 | print arr; 6 | } 7 | -------------------------------------------------------------------------------- /Chapter06/emptrystring_check.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str =""; 3 | if ( ! length(str)) 4 | print "String is empty" 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/getline_var.awk: -------------------------------------------------------------------------------- 1 | { 2 | print "$0 -> : ",NR, $0 3 | getline tmp; 4 | print "tmp -> : ",NR, tmp; 5 | } 6 | 7 | -------------------------------------------------------------------------------- /Chapter02/backreference.txt: -------------------------------------------------------------------------------- 1 | cat 2 | first example of backreferencing 3 | catcat 4 | example of palindrome 5 | xabax 6 | 7 | -------------------------------------------------------------------------------- /Chapter03/fs2.awk: -------------------------------------------------------------------------------- 1 | #!/bin/awk -f 2 | { 3 | if ( $0 ~ /:/ ) 4 | { FS=":";} 5 | else 6 | { FS=" ";} 7 | print $2 8 | } 9 | -------------------------------------------------------------------------------- /Chapter05/printf_append.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "=========employee database ends here=============\n" >>"emp_report" 3 | } 4 | -------------------------------------------------------------------------------- /Chapter07/check_zero.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | x = 0; 3 | if ( x ) 4 | print "x is non-zero" 5 | else 6 | print "x is 0" 7 | } 8 | -------------------------------------------------------------------------------- /Chapter08/getline_file.awk: -------------------------------------------------------------------------------- 1 | { 2 | print "cars.dat : ",NR,$0; 3 | getline < "emp.dat" 4 | print "emp.dat : ",NR,$0; 5 | } 6 | -------------------------------------------------------------------------------- /Chapter04/arr_forloop2.awk: -------------------------------------------------------------------------------- 1 | { 2 | arr[NR] = $0; 3 | } 4 | END { 5 | for ( v in arr ) 6 | print "arr["v"] : ",arr[v] 7 | } 8 | 9 | -------------------------------------------------------------------------------- /Chapter06/octalcheck.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | num = 4; 3 | if (num >= 0 && num <= 7) 4 | printf "%o is an octal number\n", num 5 | } 6 | -------------------------------------------------------------------------------- /Chapter07/dowhile1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | counter = 1; 3 | do 4 | print "Print this line one time" 5 | while ( counter != 1 ) 6 | } 7 | -------------------------------------------------------------------------------- /Chapter07/car_old_new.awk: -------------------------------------------------------------------------------- 1 | { 2 | if ( $3 < 2005 ) 3 | print $0, "\t", "**OLD MODEL**" 4 | else 5 | print $0, "\t", "NEW MODEL" 6 | } 7 | -------------------------------------------------------------------------------- /Chapter08/logfunc.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "log(5) = ", log(5); 3 | print "log(0) = ", log(0); 4 | print "log(-1) = ", log(-1); 5 | } 6 | -------------------------------------------------------------------------------- /Chapter09/nondecimal.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "deci\thex\toct\n" 3 | } 4 | { 5 | printf "%d\t%d\t%d\t\n", $1,$2,$3 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /Chapter09/tcpserver.awk: -------------------------------------------------------------------------------- 1 | #TCP-Server 2 | BEGIN { 3 | print strftime() |& "/inet/tcp/8080/0/0" 4 | close("/inet/tcp/8080/0/0") 5 | } 6 | -------------------------------------------------------------------------------- /Chapter03/ignorecase.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | IGNORECASE=1; 3 | } 4 | { 5 | if ( $2 == "kapur" ) print $0; 6 | if ( $1 ~ "ja" ) print $0; 7 | } 8 | -------------------------------------------------------------------------------- /Chapter04/arr_basic_split.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | z= split( "10/20/30/40", arr, "/") 3 | for ( i in arr ) 4 | print "arr["i"] : ",arr[i] 5 | } 6 | -------------------------------------------------------------------------------- /Chapter04/basic_array.awk: -------------------------------------------------------------------------------- 1 | { 2 | arr[NR] = $0 3 | } 4 | END{ 5 | for ( x=1; x<= NR; x++) 6 | print "index : "x, "value :"arr[x] 7 | } 8 | -------------------------------------------------------------------------------- /Chapter08/expfunc.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "exp(1) = ", exp(1); 3 | print "exp(0) = ", exp(0); 4 | print "exp(-12) = ", exp(-12); 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/getline_user.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "Enter your name : " 3 | getline < "-" 4 | print "Welcome to awk programming : ",$0 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/or.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | num1 = 5 3 | num2 = 6 4 | result = or(num1,num2) 5 | printf "(%d OR %d) = %d\n", num1, num2, result 6 | } 7 | -------------------------------------------------------------------------------- /Chapter06/assign.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | greet = "Welcome " ; 3 | lang = "Awk Programming"; 4 | message = greet "to " lang; 5 | print message 6 | } 7 | -------------------------------------------------------------------------------- /Chapter06/maxuid.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | FS = ":" 3 | } 4 | $3 > maxuid { maxuid=$3; userinfo=$0 }; 5 | END { 6 | print maxuid, userinfo 7 | } 8 | -------------------------------------------------------------------------------- /Chapter06/str_fname_concatenate.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | f1="sample" 3 | f2=".txt" 4 | print "this is string concatenation example" >(f1 f2) 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/and.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | num1 = 5 3 | num2 = 6 4 | result = and(num1,num2) 5 | printf "(%d AND %d) = %d\n", num1, num2, result 6 | } 7 | -------------------------------------------------------------------------------- /Chapter08/getline_var_file.awk: -------------------------------------------------------------------------------- 1 | { 2 | print "cars.dat($0) : ",NR,$0; 3 | getline tmp < "emp.dat" 4 | print "emp.dat(tmp) : ",NR,tmp; 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/gsub1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str = "Linux is derived from Unix. Unix is oldest OS." 3 | gsub("Unix", "YOONIX", str); 4 | print str; 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/rand1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "Random num1 = ", rand() 3 | print "Random num2 = ", rand() 4 | print "Random num3 = ", rand() 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/sqrtfunc.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "sqrt(5) = ", sqrt(5); 3 | print "sqrt(0) = ", sqrt(0); 4 | print "sqrt(-5) = ", sqrt(-5); 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/strftime.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | format = "Todays date is : %d-%m-%Y %H:%M:%S" 3 | 4 | print strftime( format, systime()) 5 | } 6 | 7 | 8 | -------------------------------------------------------------------------------- /Chapter08/sub2.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str = "Linux is derived from Unix. Unix is oldest OS." 3 | sub("Unix", "YOONIX", str); 4 | print str; 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/xor.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | num1 = 5 3 | num2 = 6 4 | result = xor(num1,num2) 5 | printf "(%d XOR %d) = %d\n", num1, num2, result 6 | } 7 | -------------------------------------------------------------------------------- /Chapter10/sample.txt: -------------------------------------------------------------------------------- 1 | foo baz ruby 2 | baz foo foo foo 3 | foo gold\ 4 | silver foo 5 | silver foo 6 | baz foo foo foo 7 | foo baz ruby 8 | -------------------------------------------------------------------------------- /Chapter02/interval_regex.txt: -------------------------------------------------------------------------------- 1 | a1b 2 | a12b 3 | a123b 4 | a1234b 5 | a12345b 6 | a1233456b 7 | a111b 8 | a111111b 9 | cababababt 10 | cababt 11 | -------------------------------------------------------------------------------- /Chapter03/ors.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | BEGIN { ORS="\n****************************************************************\n" } 4 | { print $0 } 5 | -------------------------------------------------------------------------------- /Chapter06/whitespacecheck.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | char = "\n" 3 | if ( char == " " || char == "\n" || char == "\t" ) 4 | print "Character is whitespace" 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/fflush.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | cmd = "wc" 3 | print "Linux is derived from Unix. Unix is oldest OS." |& cmd 4 | fflush(cmd) 5 | close(cmd); 6 | } 7 | -------------------------------------------------------------------------------- /Chapter08/getline_user_var.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "Enter your name : " 3 | getline uname < "-" 4 | print "Welcome to awk programming : ", uname 5 | } 6 | -------------------------------------------------------------------------------- /Chapter08/lshift.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | num1 = 5 3 | count =1 4 | result = lshift(num1,count) 5 | printf "lshift(%d,%d) = %d\n", num1, count, result 6 | } 7 | -------------------------------------------------------------------------------- /Chapter08/rshift.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | num1 = 5 3 | count =1 4 | result = rshift(num1,count) 5 | printf "rshift(%d,%d) = %d\n", num1, count, result 6 | } 7 | -------------------------------------------------------------------------------- /Chapter09/tcpclient.awk: -------------------------------------------------------------------------------- 1 | #TCP-client 2 | BEGIN { 3 | "/inet/tcp/0/localhost/8080" |& getline 4 | print $0 5 | close("/inet/tcp/0/localhost/8080") 6 | } 7 | -------------------------------------------------------------------------------- /Chapter10/table.txt: -------------------------------------------------------------------------------- 1 | NAME:ENGLISH:MATHS:SOCIAL SCIENCE:MUSIC 2 | Sanjay:70:80:65:95 3 | Hitesh:55:72:82:64 4 | Rahul:80:83:65:70 5 | Dhirendra:81:82:83:84 6 | -------------------------------------------------------------------------------- /Chapter03/arguments.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "Total no. of arguments =", ARGC 3 | for ( i=0; i 60 ) 3 | { 4 | exit 2 5 | } 6 | else if ( length($0) < 60 ) 7 | { 8 | exit 1 9 | } 10 | print 11 | } 12 | -------------------------------------------------------------------------------- /Chapter09/small2capital.awk: -------------------------------------------------------------------------------- 1 | { 2 | cmd = "tr [a-z] [A-Z]" 3 | print $0 |& cmd 4 | close(cmd,"to") 5 | cmd |& getline 6 | print $0 7 | close(cmd); 8 | } 9 | -------------------------------------------------------------------------------- /Chapter03/strings.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | 3 | if ( "0" ) 4 | print "string 0"; 5 | if ("one") 6 | print "string one" 7 | if ("") 8 | print "empty string" 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Chapter05/names.sorted: -------------------------------------------------------------------------------- 1 | Amit 2 | Amy 3 | Ana 4 | Billy 5 | Emily 6 | Eva 7 | Ginny 8 | Hari 9 | Jack 10 | Jane 11 | John 12 | Julie 13 | Sam 14 | Victor 15 | Vina 16 | -------------------------------------------------------------------------------- /Chapter07/for3.awk: -------------------------------------------------------------------------------- 1 | { 2 | total=0; 3 | for ( i=2; i <=NF; i++ ) 4 | { 5 | total = total + $i; 6 | } 7 | print "Student Name : ",$1, "\t", "Total Marks : ", total; 8 | } 9 | -------------------------------------------------------------------------------- /Chapter08/func4.awk: -------------------------------------------------------------------------------- 1 | function demo(){ 2 | abc = "Welcome to awk" 3 | return abc 4 | } 5 | 6 | 7 | BEGIN { 8 | myfunc = "demo" 9 | print @myfunc() 10 | } 11 | 12 | -------------------------------------------------------------------------------- /Chapter05/printf_symbol.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "|%-10s|%-8s|\n", "FNAME","SALARY" 3 | printf "|==========|========|\n" 4 | } 5 | { 6 | printf "|%-10s|$%-7d|\n", $1,$7 7 | } 8 | -------------------------------------------------------------------------------- /Chapter07/break1.awk: -------------------------------------------------------------------------------- 1 | { 2 | i=1; 3 | while ( i <= NF ) 4 | { 5 | if ( $i == 1995 ) 6 | { 7 | print $0,"OLD MODEL" 8 | break; 9 | } 10 | i++ 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Chapter08/getline_pipe2.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | 3 | ("echo " "Hello World..!") | getline 4 | print "Msg : ", $0 5 | close("echo " "Hello World..!") 6 | } 7 | 8 | -------------------------------------------------------------------------------- /Chapter08/getline_var_pipe.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | 3 | "date" | getline current_time 4 | print "Date is : ", current_time 5 | close("current_time") 6 | } 7 | 8 | -------------------------------------------------------------------------------- /Chapter01/validate.awk: -------------------------------------------------------------------------------- 1 | NF !=5 { print $0, "number of fields is not equal to 3" } 2 | $3 < 2000 { print $0, "car fitness expired" } 3 | $3 > 2009 { print $0, "Better car for resale" } 4 | -------------------------------------------------------------------------------- /Chapter03/fs1.awk: -------------------------------------------------------------------------------- 1 | #!/bin/awk -f 2 | { 3 | if ($1 == "#entry") 4 | { FS=":"; } 5 | else if ($1 == "#exit") 6 | { FS=" "; } 7 | else 8 | { print $2 } 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Chapter03/nf2.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | { 3 | if (NF == 7) 4 | { 5 | print $1, $3; 6 | } 7 | else if (NF == 6) 8 | { 9 | print $1, $2; 10 | } 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /Chapter05/printf_leading_zero.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "|%-.4s|%-.2s|\n", "FNAME", "SALARY" 3 | printf "|=====|=====|\n" 4 | } 5 | { 6 | printf "|%-.4s|%-.3d|\n", $1, $7 7 | } 8 | -------------------------------------------------------------------------------- /Chapter05/printf_positional_modifier.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "%2$s\t%1$s\n", 3 | "FName","Sex" 4 | printf "-----------\n" 5 | } 6 | { 7 | printf "%2$s\t%1$s\n", $1,$5 8 | } 9 | -------------------------------------------------------------------------------- /Chapter07/next.awk: -------------------------------------------------------------------------------- 1 | { 2 | for ( i = 1; i <= NF; i++ ) 3 | { 4 | if ( $i == "akshat" ) 5 | { 6 | next 7 | } 8 | printf ( "%s\t", $i ) 9 | } 10 | printf ("\n") 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/gensub3.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str = "hello:world" 3 | newstr = gensub(/(.+):(.+)/, "\\2:\\1", "g", str) 4 | print "OLD STRING : ", str 5 | print "NEW STRING : ", newstr 6 | } 7 | -------------------------------------------------------------------------------- /Chapter09/2wayio.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | cmd = "tr [a-z] [A-Z]" 3 | print "hello, world !!!" |& cmd 4 | close(cmd,"to") 5 | cmd |& getline 6 | print $0 7 | close(cmd); 8 | } 9 | -------------------------------------------------------------------------------- /Chapter09/udpserver.awk: -------------------------------------------------------------------------------- 1 | #UDP-Server 2 | BEGIN { 3 | print strftime() |& "/inet/udp/8080/0/0" 4 | "/inet/udp/8080/0/0" |& getline 5 | print $0 6 | close("/inet/udp/8080/0/0") 7 | } 8 | -------------------------------------------------------------------------------- /Chapter02/gopr.txt: -------------------------------------------------------------------------------- 1 | start 2 | bigstarting 3 | starting 4 | start end 5 | white space example 6 | e d 7 | end 8 | e_d 9 | e1d 10 | e;d 11 | eNd 12 | white space example with tab 13 | e d 14 | -------------------------------------------------------------------------------- /Chapter04/marks.txt: -------------------------------------------------------------------------------- 1 | ram 80 78 60 85 72 2 | amit 64 67 69 61 62 3 | vijay 90 98 92 96 97 4 | satvik 81 74 72 79 80 5 | akshat 67 80 74 60 72 6 | rishi 85 80 82 76 84 7 | tushar 70 82 68 79 60 8 | -------------------------------------------------------------------------------- /Chapter05/printf_example.awk: -------------------------------------------------------------------------------- 1 | BEGIN { printf "FNAME EMAIL_ID\n" 2 | printf "================\n" 3 | } 4 | { 5 | printf "%s\t%s\n", $1, $4 6 | } 7 | 8 | END { printf "================\n" } 9 | -------------------------------------------------------------------------------- /Chapter06/marks.txt: -------------------------------------------------------------------------------- 1 | ram 80 78 60 85 72 2 | amit 64 67 69 61 62 3 | vijay 90 98 92 96 97 4 | satvik 81 74 72 79 80 5 | akshat 67 80 74 60 72 6 | rishi 85 80 82 76 84 7 | tushar 70 82 68 79 60 8 | -------------------------------------------------------------------------------- /Chapter07/continue2.awk: -------------------------------------------------------------------------------- 1 | { 2 | for ( x=1; x <=NF; x++ ) 3 | { 4 | if ( x == 2 || x == 5 || x == 6 || x == 7 ) 5 | continue 6 | printf( "%s\t", $x ) 7 | } 8 | printf("\n") 9 | } 10 | -------------------------------------------------------------------------------- /Chapter07/if_else_if1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | p = 20; 3 | if ( p == 10 ) 4 | print "p = 10" 5 | else if ( p == 20 ) 6 | print "p = 20" 7 | else if ( p == 30 ) 8 | print "p = 30" 9 | } 10 | -------------------------------------------------------------------------------- /Chapter07/marks.txt: -------------------------------------------------------------------------------- 1 | ram 80 78 60 85 72 2 | amit 64 67 69 61 62 3 | vijay 90 98 92 96 97 4 | satvik 81 74 72 79 80 5 | akshat 67 80 74 60 72 6 | rishi 85 80 82 76 84 7 | tushar 70 82 68 79 60 8 | -------------------------------------------------------------------------------- /Chapter07/while3.awk: -------------------------------------------------------------------------------- 1 | { 2 | i=2; total=0; 3 | while ( i <=NF ) 4 | { 5 | total = total +$i; 6 | i++; 7 | } 8 | print "Student Name : ",$1, "\t", "Total Marks : ", total; 9 | } 10 | -------------------------------------------------------------------------------- /Chapter08/marks.txt: -------------------------------------------------------------------------------- 1 | ram 80 78 60 85 72 2 | amit 64 67 69 61 62 3 | vijay 90 98 92 96 97 4 | satvik 81 74 72 79 80 5 | akshat 67 80 74 60 72 6 | rishi 85 80 82 76 84 7 | tushar 70 82 68 79 60 8 | -------------------------------------------------------------------------------- /Chapter09/marks.txt: -------------------------------------------------------------------------------- 1 | ram 80 78 60 85 72 2 | amit 64 67 69 61 62 3 | vijay 90 98 92 96 97 4 | satvik 81 74 72 79 80 5 | akshat 67 80 74 60 72 6 | rishi 85 80 82 76 84 7 | tushar 70 82 68 79 60 8 | -------------------------------------------------------------------------------- /Chapter10/marks.txt: -------------------------------------------------------------------------------- 1 | ram 80 78 60 85 72 2 | amit 64 67 69 61 62 3 | vijay 90 98 92 96 97 4 | satvik 81 74 72 79 80 5 | akshat 67 80 74 60 72 6 | rishi 85 80 82 76 84 7 | tushar 70 82 68 79 60 8 | -------------------------------------------------------------------------------- /Chapter03/subsep.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | SUBSEP=":"; 3 | item[1,1]=100; 4 | item[1,2]=200; 5 | item[2,1]=300; 6 | item[2,2]=400; 7 | for (x in item) 8 | print "Index",x,"contains",item[x]; 9 | } 10 | -------------------------------------------------------------------------------- /Chapter07/ifelse1.awk: -------------------------------------------------------------------------------- 1 | { 2 | if ( NR%2 ) 3 | { 4 | OFS="="; 5 | print NR,$1,$2,$3,$4,$5,$6,$7; 6 | } 7 | else 8 | { 9 | OFS="||"; 10 | print NR,$1,$2,$3,$4,$5,$6,$7; 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Chapter08/close1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | cmd = "wc" 3 | print "Linux is derived from Unix. Unix is oldest OS." |& cmd 4 | close(cmd, "to") 5 | cmd |& getline var 6 | print var 7 | close(cmd); 8 | } 9 | -------------------------------------------------------------------------------- /Chapter03/mod_subsep.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | SUBSEP=":"; 3 | item["1,1"]=100; 4 | item["1,2"]=200; 5 | item[2,1]=300; 6 | item[2,2]=400; 7 | for (x in item) 8 | print "Index",x,"contains",item[x]; 9 | } 10 | -------------------------------------------------------------------------------- /Chapter05/printf_hash_modifier.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "Octal representation = %#o\n", 10 3 | printf "Hexadecimal representaion = %#X\n", 10 4 | printf "Trailing zeros in %% g = %#g\n", 10 5 | } 6 | -------------------------------------------------------------------------------- /Chapter07/dowhile2.awk: -------------------------------------------------------------------------------- 1 | { 2 | i=2; total=0; 3 | do 4 | { 5 | total = total + $i; 6 | i++; 7 | } 8 | while ( i <= NF ) 9 | print "Student Name : ", $1, "\t", "Total Marks :", total; 10 | } 11 | -------------------------------------------------------------------------------- /Chapter07/if_else_if2.awk: -------------------------------------------------------------------------------- 1 | { 2 | if ( $4 ~ /gmail/ ) 3 | print $4 > "gmail.txt" 4 | else if ( $4 ~ /yahoo/ ) 5 | print $4 > "yahoo.txt" 6 | else if ( $4 ~ /hotmail/ ) 7 | print $4 > "hotmail.txt" 8 | } 9 | -------------------------------------------------------------------------------- /Chapter01/header.awk: -------------------------------------------------------------------------------- 1 | #BEGIN { print "Make" "\t\t" "Model" "\t\t" "Year" "\t" "Kms" "\t" "Price" ; print "" } 2 | BEGIN { print "Make Model Year Kms Price"; print ""} 3 | {print} 4 | 5 | -------------------------------------------------------------------------------- /Chapter03/numeric.awk: -------------------------------------------------------------------------------- 1 | BEGIN{ 2 | if ( 1 ) 3 | print "numeric 1 " 4 | if ( 1.234 ) 5 | print "numeric 1.234" 6 | if ( 1.234e ) 7 | print "numeric 1.234e" 8 | if ( 0 ) 9 | print "false" 10 | } 11 | 12 | -------------------------------------------------------------------------------- /Chapter08/gensub1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str = "Linux is derived from Unix. Unix is oldest OS." 3 | newstr = gensub( /Unix/, "YOONIX", "g", str) 4 | print "NEW STRING : "newstr; 5 | print "OLD STRING : "str; 6 | } 7 | -------------------------------------------------------------------------------- /Chapter08/getline_var_coprocess.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | cmd = "tr [a-z] [A-Z]" 3 | print "Linux is derived from Unix. Unix is oldest OS" |& cmd 4 | close(cmd, "to") 5 | cmd |& getline 6 | print $0 7 | close(cmd) 8 | } 9 | -------------------------------------------------------------------------------- /Chapter08/tolower1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str = "Linux is derived from Unix. Unix is oldest OS." 3 | lower_case_str = tolower(str) 4 | print "Original String : ", str 5 | print "Lowercase String : ", lower_case_str 6 | } 7 | -------------------------------------------------------------------------------- /Chapter08/toupper1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str = "Linux is derived from Unix. Unix is oldest OS." 3 | upper_case_str = toupper(str) 4 | print "Original String : ", str 5 | print "Uppercase String : ", upper_case_str 6 | } 7 | -------------------------------------------------------------------------------- /Chapter09/udpclient.awk: -------------------------------------------------------------------------------- 1 | #UDP-client 2 | BEGIN { 3 | print "hello from client!" |& "/inet/udp/0/localhost/8080" 4 | "/inet/udp/0/localhost/8080" |& getline 5 | print $0 6 | close("/inet/udp/0/localhost/8080") 7 | } 8 | -------------------------------------------------------------------------------- /Chapter01/displayargs.awk: -------------------------------------------------------------------------------- 1 | # echo - print command-line arguments 2 | BEGIN { 3 | printf "No. of command line args is : %d\n", ARGC-1; 4 | for (i = 1; i < ARGC; i++) 5 | printf "ARG[%d] is : %s \n", i, ARGV[i] 6 | } 7 | 8 | -------------------------------------------------------------------------------- /Chapter03/erro.awk: -------------------------------------------------------------------------------- 1 | { 2 | x=getline < "dummy-file.txt" 3 | if ($1 == "Eva") 4 | { 5 | print "Trying to read from file : dummy-file.txt" 6 | if ( x == -1 ) 7 | print ERRNO 8 | else 9 | print $0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter07/break3.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | x=1 3 | while(1) 4 | { 5 | print x,"iteration" 6 | if ( x == 10 ) 7 | { 8 | break 9 | } 10 | x++ 11 | } 12 | print "This statement is outside while loop..!" 13 | } 14 | -------------------------------------------------------------------------------- /Chapter08/strtonum.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "Decimal num strtonum(123) : ", strtonum(123) 3 | print "Octal num strtonum(0123) : ", strtonum(0123) 4 | print "Hexadecimal num strtonum(0x123) : ", strtonum(0x123) 5 | } 6 | 7 | -------------------------------------------------------------------------------- /Chapter09/view_webpage.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | RS=ORS="\r\n" 3 | http = "/inet/tcp/0/35.164.82.168/80" 4 | print "GET http://35.164.82.168/Unix/donate.html" |& http 5 | while ((http |& getline) > 0) 6 | print $0 7 | close(http) 8 | } 9 | -------------------------------------------------------------------------------- /Chapter10/transpose_matrix.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | FS = ";" 3 | } 4 | { 5 | for( i=1; i<=NF; i++ ) 6 | r[i]=r[i] sep $i 7 | sep=FS 8 | } 9 | 10 | END { 11 | for( i=1; i<=NF; i++ ) 12 | { 13 | print r[i] 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Chapter08/length1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "Example of length calculation of string" 3 | print "length(hello..!) : " length("hello..!") 4 | print "Example of length calculation of number" 5 | print "length(10*20) : " length(10*20) 6 | } 7 | -------------------------------------------------------------------------------- /Chapter02/dot_regex.txt: -------------------------------------------------------------------------------- 1 | Lets go for a walk 2 | Singing is good hobby 3 | We will talk on this matter 4 | Ping me when you are free 5 | (that is cool) 6 | My son birthday is on 24/04/14 7 | I will be going to Singapore on 24-04-14 8 | (this) 9 | -------------------------------------------------------------------------------- /Chapter03/fnr.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "Example to show both NR and FNR difference..!" 3 | } 4 | { 5 | printf "FILENAME=%s NR=%s FNR=%s\n", FILENAME, NR, FNR; 6 | } 7 | END { 8 | printf "END Block: NR=%s FNR=%s\n", NR, FNR 9 | } 10 | -------------------------------------------------------------------------------- /Chapter04/arr_delete.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | arr[10] = "maruti" 3 | arr[20] = "audi" 4 | arr["car"] = "ford" 5 | arr[30] = "ferrari" 6 | arr[40] = "porsche" 7 | 8 | delete arr["car"] 9 | for ( v in arr ) 10 | print v, arr[v] 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/intfunc.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "int(3.5678) = ", int(3.5678); 3 | print "int(5.9876) = ", int(5.9876); 4 | print "int(4) = ", int(4); 5 | print "int(-3.1234)= ", int(-3.1234); 6 | print "int(-4) = ", int(-4); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /Chapter08/length2.awk: -------------------------------------------------------------------------------- 1 | { 2 | if (length($0) < 80) 3 | { 4 | prefix = ""; 5 | for (i = 1; i < (80-length($0)) / 2 ;i++) 6 | prefix = prefix " "; 7 | print prefix $0; 8 | } 9 | else 10 | { 11 | print; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Chapter04/multi_arr1.awk: -------------------------------------------------------------------------------- 1 | BEGIN{ 2 | arr["1,1"] = 10 3 | arr["1,2"] = 20 4 | arr["2,1"] = 30 5 | arr["2,2"] = 40 6 | arr["3,1"] = 50 7 | arr["3,2"] = 60 8 | 9 | for ( v in arr ) 10 | print "Index ",v, " contains "arr[v] 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/rand2.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | i=0; 3 | while( i <= 10 ) 4 | { 5 | n=int(rand()*100); 6 | rand_num[i]=n; 7 | i++; 8 | } 9 | for ( i=0; i<=10; i++ ) 10 | { 11 | print "Random number is : ", rand_num[i] ; 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Chapter05/close.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | cmd = "date" 3 | cmd |& getline 4 | print "DATE : ", $0 5 | close(cmd) 6 | cmd = "sort cars.dat" 7 | while (( cmd |& getline ) > 0 ) 8 | print $0 9 | close(cmd); 10 | print "End of File..!" 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Chapter04/multi_arr2.awk: -------------------------------------------------------------------------------- 1 | BEGIN{ 2 | SUBSEP = ":" 3 | arr["1,1"] = 10 4 | arr["1,2"] = 20 5 | arr["2,1"] = 30 6 | arr["2,2"] = 40 7 | arr[3,1] = 50 8 | arr[3,2] = 60 9 | 10 | for ( v in arr ) 11 | print "Index ",v, " contains "arr[v] 12 | } 13 | -------------------------------------------------------------------------------- /Chapter04/arr_del_forloop.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | arr[10] = "maruti" 3 | arr[20] = "audi" 4 | arr["car"] = "ford" 5 | arr[30] = "ferrari" 6 | arr[40] = "porsche" 7 | 8 | for ( v in arr ) 9 | delete arr[v] 10 | for ( v in arr ) 11 | print arr[v] 12 | } 13 | -------------------------------------------------------------------------------- /Chapter06/string.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | OFS="," 3 | str1 = "Good" 4 | str2 = "Morning" 5 | num1 = "10" 6 | str3 = str1 " " str2; 7 | print "Concatenated string is : " str3 8 | num1 = num1+1 9 | print "string to number conversion on addition : " num1 10 | } 11 | -------------------------------------------------------------------------------- /Chapter07/exit1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | x=1 3 | while(1) 4 | { 5 | print x,"iteration" 6 | if ( x == 10 ) 7 | { 8 | exit(5) 9 | } 10 | x++ 11 | } 12 | print "This statement is outside while loop..!" 13 | } 14 | END { print "Inside exit statement" } 15 | -------------------------------------------------------------------------------- /Chapter05/printf_hex.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "Lower Case Letters using : x\n" 3 | for ( i = 1 ; i <= 15; ++i ) 4 | printf "%x ", i 5 | printf "\nUpper Case Letters using : X\n" 6 | for ( i = 1 ; i <= 15; ++i ) 7 | printf "%X ", i 8 | printf "\n" 9 | } 10 | -------------------------------------------------------------------------------- /Chapter06/sum.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "%-6s\t%-7s\t%-7s\t%-7s\t%-7s\t%-7s\t%-5s\n", "Name", "Eng","Hindi","Maths","Science","Arts","Total" 3 | } 4 | { 5 | sum = $2+$3+$4+$5+$6; 6 | printf "%-6s\t%-7d\t%-7d\t%-7d\t%-7d\t%-7d\t%-5d\n", $1,$2,$3,$4,$5,$6,sum 7 | } 8 | 9 | -------------------------------------------------------------------------------- /Chapter08/split1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | string = "one-two-three-four" 3 | regex = "-" 4 | n = split ( string, arr, regex ) 5 | 6 | print "Array contains the following values: " 7 | for ( i=1; i<=n; i++ ) 8 | { 9 | printf("arr[%d] : %s\n", i, arr[i]) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/sub1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str = "Unix is Beutiful" 3 | original_substring = "Unix" 4 | replacement_substring = "Awk" 5 | print "String before replacemnt = " str 6 | sub( original_substring, replacement_substring, str) 7 | print "String after replacement = " str 8 | } 9 | -------------------------------------------------------------------------------- /Chapter04/arr_delall.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | arr[10] = "maruti" 3 | arr[20] = "audi" 4 | arr["car"] = "ford" 5 | arr[30] = "ferrari" 6 | arr[40] = "porsche" 7 | 8 | delete arr 9 | print "List of elements in array is : " 10 | for ( v in arr ) 11 | print arr[v] 12 | } 13 | -------------------------------------------------------------------------------- /Chapter04/arr_forloop.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | arr[10] = "bmw" 3 | arr[20] = "audi" 4 | arr[30] = "volvo" 5 | arr[40] = "ford" 6 | arr[50] = "toyota" 7 | arr[60] = "jeep" 8 | arr[70] = "renault" 9 | arr[80] = "porsche" 10 | for ( v in arr) 11 | print v, arr[v]; 12 | } 13 | -------------------------------------------------------------------------------- /Chapter08/split2.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | string = "one:two+three=four" 3 | regex = "[:,+,=]" 4 | n = split ( string, arr, regex ) 5 | 6 | print "Array contains the following values: " 7 | for ( i=1; i<=n; i++ ) 8 | { 9 | printf("arr[%d] : %s\n", i, arr[i]) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/test.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | string = "one:two+three=four" 3 | regex = "[:,+,=]" 4 | n = split ( string, arr, regex ) 5 | 6 | print "Array contains the following values: " 7 | for ( i=1; i<=n; i++ ) 8 | { 9 | printf("arr[%d] : %s\n", i, arr[i]) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter01/empinfo.txt: -------------------------------------------------------------------------------- 1 | Jack 9857532312 jack@gmail.com hr 2000 5 2 | Jane 9837432312 jane@gmail.com hr 1800 5 3 | Eva 8827232115 eva@gmail.com lgs 2100 6 4 | amit 9911887766 amit@yahoo.com lgs 2350 6 5 | Julie 8826234556 julie@yahoo.com hr 2500 5 6 | -------------------------------------------------------------------------------- /Chapter03/txt2csv.awk: -------------------------------------------------------------------------------- 1 | # txt2csv - Convert a data file into comma-separated format 2 | 3 | BEGIN { 4 | IFS="\t" # input field separator is a tab, u can skip this also for current file 5 | OFS="," # output field separator is a comma 6 | } 7 | 8 | { print $1, $2, $3, $4, $5 } 9 | -------------------------------------------------------------------------------- /Chapter04/multi_arr3.awk: -------------------------------------------------------------------------------- 1 | BEGIN{ 2 | SUBSEP = ":" 3 | arr[1,1] = 10 4 | arr[1,2] = 20 5 | arr[2,1] = 30 6 | arr[2,2] = 40 7 | arr[3,1] = 50 8 | arr[3,2] = 60 9 | 10 | if ( (2 SUBSEP 1) in arr ) 11 | print "arr[2,1] is : " arr[2,1] 12 | else 13 | print "index not found" 14 | } 15 | -------------------------------------------------------------------------------- /Chapter07/switch1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | p = "10"; 3 | switch ( p ) { 4 | case "a" : 5 | print "p = a" 6 | break 7 | case "20" : 8 | print "p = 20" 9 | break 10 | case "10" : 11 | print "p = 10" 12 | break 13 | default : 14 | print "No match" 15 | break 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Chapter08/match1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | 3 | str = "linux is derived from UNIX. UNIX is oldest OS." 4 | position = match( str, /derived/ ) 5 | print "String : ",str 6 | print "Starting Position of matched string : ",position 7 | print "RSTART : ", RSTART 8 | print "RLENGTH : ", RLENGTH 9 | } 10 | -------------------------------------------------------------------------------- /Chapter08/srand2.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | srand(5); 3 | total_num=5; 4 | count=0; 5 | while( count < total_num ) 6 | { 7 | n=int(rand()*50); 8 | rand_num[count]=n; 9 | count++; 10 | } 11 | asort(rand_num) 12 | for ( i in rand_num ) 13 | { 14 | print rand_num[i] 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Chapter08/func2.awk: -------------------------------------------------------------------------------- 1 | function one(p) 2 | { 3 | result = p + q 4 | print "p + q : ", result 5 | print "local variable \"p\" : ", p 6 | print "global variable \"q\" : ", q 7 | } 8 | 9 | BEGIN { 10 | q = 10 11 | one(5) 12 | print "value of p : ", p 13 | print "value of q : ", q 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Chapter08/systime.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "Timestamp of program run : ", systime() 3 | LOOPS=10000000; 4 | start=systime(); 5 | print start; 6 | for (i=0;i "gmail.txt" 5 | break 6 | case /yahoo/ : 7 | print $4 > "yahoo.txt" 8 | break 9 | case /hotmail/ : 10 | print $4 > "hotmail.txt" 11 | break 12 | default : 13 | print $4 > "misc_id.txt" 14 | break 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /Chapter08/index.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str = "Awk is a powerful utility. Unix is awesome." 3 | search = "power" 4 | location = index(str, search) 5 | printf("Substring \"%s\" found at location : %d\n", search, location) 6 | 7 | if ( index(str, ",") == 0 ) 8 | { 9 | print "Comma (,) not found in sentence" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter08/strftime1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | current_time = systime(); 3 | timestamp = (7*24*60*60) + current_time 4 | format = "%d-%m-%Y %H:%M:%S" 5 | present_time = strftime ( format, current_time ) 6 | new_time = strftime( format, timestamp ) 7 | print "Current Time : ", present_time 8 | print "Next Week : ", new_time 9 | } 10 | -------------------------------------------------------------------------------- /Chapter05/printf_width.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "%6s\t%6s\t%10s\t%17s\t%3s\t%3s\t%6s\n", 3 | "FName","LName","ContactNo.","EmailId","Sex","Dpt","Salary" 4 | printf "------------------------------------------------------------------------------\n" 5 | } 6 | { 7 | printf "%6s\t%6s\t%10d\t%17s\t%3s\t%3s\t%4d\n", $1,$2,$3,$4,$5,$6,$7 8 | } 9 | 10 | -------------------------------------------------------------------------------- /Chapter09/asort_arr.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | arr[30] = "volvo" 3 | arr[10] = "bmw" 4 | arr[20] = "audi" 5 | arr[50] = "toyota" 6 | arr["car"] = "ferrari" 7 | arr[70] = "renault" 8 | arr[110] = 20 9 | arr[40] = "ford" 10 | arr["num"] = 10 11 | arr[80] = "porsche" 12 | arr[60] = "jeep" 13 | 14 | n = asort(arr) 15 | for ( v in arr ) 16 | print v, arr[v] 17 | } 18 | -------------------------------------------------------------------------------- /Chapter09/asorti_1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | arr[30] = "volvo" 3 | arr[10] = "bmw" 4 | arr[20] = "audi" 5 | arr[50] = "toyota" 6 | arr["car"] = "ferrari" 7 | arr[70] = "renault" 8 | arr[110] = 20 9 | arr[40] = "ford" 10 | arr["num"] = 10 11 | arr[80] = "porsche" 12 | arr[60] = "jeep" 13 | 14 | n = asorti(arr) 15 | for ( v in arr ) 16 | print v, arr[v] 17 | } 18 | -------------------------------------------------------------------------------- /Chapter03/fnr2.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | BEGIN{ f1=""; 4 | f2=""; 5 | } 6 | { 7 | if ( FILENAME == "cars.dat" ) 8 | { 9 | print NR, $1,"\t", $2 ; 10 | f1=FILENAME; 11 | } 12 | else 13 | { 14 | print; 15 | f2=FILENAME; 16 | } 17 | } 18 | END{ 19 | print "First file processed is ", f1; 20 | print "Second file processed is ", f2; 21 | } 22 | -------------------------------------------------------------------------------- /Chapter03/argc_argv.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | OFS="\t" 3 | for ( i=0; i 2 { 3 | idle = $4 4 | sub(/^ +/, "", idle) # strip leading spaces 5 | if (idle == "") 6 | idle = 0 7 | if (idle ~ /:/) { # hh:mm 8 | split(idle, t, ":") 9 | idle = t[1] * 60 + t[2] 10 | } 11 | if (idle ~ /days/) 12 | idle *= 24 * 60 * 60 13 | 14 | print $1, $2, idle 15 | } 16 | -------------------------------------------------------------------------------- /Chapter05/printf_precision.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | print "====Using .1 precision====" 3 | printf ".1d -> %.1d\n", 201.33 4 | printf ".1e -> %.1e\n", 201.33 5 | printf ".1f -> %.1f\n", 201.33 6 | printf ".1g -> %.1g\n", 201.33 7 | print "====Using .4 precision====" 8 | printf ".4d -> %.4d\n", 201.33 9 | printf ".4e -> %.4e\n", 201.33 10 | printf ".4f -> %.4f\n", 201.33 11 | printf ".4g -> %.4g\n", 201.33 12 | } 13 | -------------------------------------------------------------------------------- /Chapter04/multi_arr4.awk: -------------------------------------------------------------------------------- 1 | BEGIN{ 2 | SUBSEP = ":" 3 | arr[1,1] = 10 4 | arr[1,2] = 20 5 | arr[2,1] = 30 6 | arr[2,2] = 40 7 | arr[3,1] = 50 8 | arr[3,2] = 60 9 | 10 | delete arr # delete arrays using single delete command 11 | print "Now printing array if exists...!" 12 | for ( v in arr ) 13 | print arr[v] 14 | 15 | arr[i,j] = 100 16 | print arr[i,j] 17 | delete arr[i,j] # delete arrays using indexes 18 | } 19 | -------------------------------------------------------------------------------- /Chapter10/transpose2.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | FS = ";"; 3 | max_x =0; 4 | max_y =0; 5 | } 6 | 7 | { 8 | max_y++; 9 | for( i=1; i<=NF; i++ ) 10 | { 11 | if (i>max_x) max_x=i; 12 | A[i,max_y] = $i; 13 | } 14 | } 15 | 16 | END { 17 | for ( x=1; x<=max_x; x++ ) 18 | { 19 | for ( y=1; y<=max_y; y++ ) 20 | { 21 | if ( (x,y) in A ) printf "%s",A[x,y]; 22 | if ( y!=max_y ) printf ";"; 23 | } 24 | printf "\n"; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Chapter08/getline_pipe.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | 3 | "date" | getline 4 | print "Date is : ", $0 5 | close("date") 6 | print "=====================" 7 | print "DISK FREE SPACE INFO" 8 | print "=====================" 9 | 10 | # using loop to print multiple lines 11 | while (("df -h" | getline) > 0 ) 12 | print "Disk info : ", $0 13 | close("df -h") 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Chapter07/dowhile3.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | FS=":" 3 | } 4 | { 5 | 6 | i=1 7 | while ( i<=7 ) 8 | { 9 | if ( i == 1 ) 10 | { printf "User Name \t: %s\n", $i } 11 | else if ( i == 3 ) 12 | { printf "UID \t\t: %s\n", $i } 13 | else if ( i == 6 ) 14 | { printf "Home Directory \t: %s\n", $i } 15 | else if ( i == 7 ) 16 | { printf "Default Shell \t: %s\n", $i 17 | printf "************************************\n" 18 | } 19 | i++ 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter01/emp_comment.awk: -------------------------------------------------------------------------------- 1 | #!/usr/bin/awk -f 2 | 3 | # Info : This program displays the employees information 4 | # Date : 09 Sept 2017 5 | # Version : 1.0 6 | # Author : Shiwang 7 | 8 | # Header part is defined in BEGIN block to display as Company information here 9 | 10 | BEGIN { print "****Employee Information of HMT Corp.****" } 11 | { print } # Body block 12 | END { print "***Information Database ends here****" } # End block comment 13 | 14 | 15 | -------------------------------------------------------------------------------- /Chapter10/dec2bin.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | Print "\tDisplay Binary equivalent of Decimal number " 3 | } 4 | 5 | func getnumber(decimal,temp, binary) 6 | { 7 | binary = ""; 8 | temp=decimal; 9 | while( temp ) 10 | { 11 | if ( temp%2==0 ) 12 | { 13 | binary = "0" binary; 14 | } 15 | else 16 | { 17 | binary = "1" binary; 18 | } 19 | temp = int(temp/2); 20 | } 21 | return binary; 22 | } 23 | { 24 | binval=getnumber( $1 ); 25 | print $1, " --> ", binval 26 | } 27 | -------------------------------------------------------------------------------- /Chapter10/emp2.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 3 | new line one 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 6 | new line two 7 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 8 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 9 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 10 | -------------------------------------------------------------------------------- /Chapter01/seq.awk: -------------------------------------------------------------------------------- 1 | # Program to print sequences of integers 2 | 3 | BEGIN { 4 | # If only one argument is given start from number 1 5 | if (ARGC == 2) 6 | for (i = 1; i <= ARGV[1]; i++) 7 | print i 8 | # If 2 arguments are given start from first number upto second number 9 | else if (ARGC == 3) 10 | for (i = ARGV[1]; i <= ARGV[2]; i++) 11 | print i 12 | # If 3 arguments are given start from first number through second with a stepping of third number 13 | else if (ARGC == 4) 14 | for (i = ARGV[1]; i <= ARGV[2]; i += ARGV[3]) 15 | print i 16 | } 17 | -------------------------------------------------------------------------------- /Chapter03/ofmt.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | A=123.123456789 3 | print "---Default OFMT---"; 4 | print A; 5 | print "====================="; 6 | 7 | OFMT="%.4g"; 8 | print "---%.4g as OFMT---"; 9 | print A; 10 | print "====================="; 11 | 12 | OFMT="%.8g"; 13 | print "---%.8g as OFMT---"; 14 | print A; 15 | print "====================="; 16 | 17 | OFMT="%2.2f"; 18 | print "---%.2.2f as OFMT---"; 19 | print A; 20 | print "====================="; 21 | 22 | OFMT="%d"; 23 | print "---%d as OFMT---"; 24 | print A; 25 | print "====================="; 26 | } 27 | -------------------------------------------------------------------------------- /Chapter09/calc.awk: -------------------------------------------------------------------------------- 1 | function find_add(num1, num2){ 2 | result = num1 + num2 3 | printf ("Addition of %d + %d : %d\n", num1,num2,result) 4 | } 5 | 6 | function find_sub(num1, num2){ 7 | result = num1 - num2 8 | printf ("Subtraction of %d - %d : %d\n", num1,num2,result) 9 | } 10 | 11 | function find_mul(num1, num2){ 12 | result = num1 * num2 13 | printf ("Multiplication of %d * %d : %d\n", num1,num2,result) 14 | } 15 | 16 | # Main function 17 | function calc(){ 18 | find_add(30,10) 19 | find_sub(40,10) 20 | find_mul(5,6) 21 | } 22 | 23 | 24 | BEGIN { 25 | 26 | calc() 27 | } 28 | -------------------------------------------------------------------------------- /Chapter08/substr1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | str = "Winter is coming.!" 3 | print "STRING IS : ", str 4 | # substr function with start position and length 5 | printf("substr( str, 5, 5 ) : %s\n", substr( str, 5, 5 )); 6 | # substr functon with start position only 7 | printf("substr( str, 5 ) : %s\n", substr( str, 5 )); 8 | # substr function with less than 1 as start position is treated as 1 9 | printf("substr( str, -1, 5 ) : %s\n", substr( str, -1, 5 )); 10 | # substr function with start position more than number of char in string returns null string 11 | printf("substr( str, 25, 5 ) : %s\n", substr( str, 25, 5 )); 12 | } 13 | -------------------------------------------------------------------------------- /Chapter03/2convfmt.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | A=123.123456789 3 | print "---Default CONVFMT---"; 4 | printf "%s\n", A; 5 | print "====================="; 6 | 7 | CONVFMT="%.4g"; 8 | print "---%.4g as CONVFMT---"; 9 | printf "%s\n", A; 10 | print "====================="; 11 | 12 | CONVFMT="%.8g"; 13 | print "---%.8g as CONVFMT---"; 14 | printf "%s\n", A; 15 | print "====================="; 16 | 17 | CONVFMT="%2.2f"; 18 | print "---%.2.2f as CONVFMT---"; 19 | printf "%s\n", A; 20 | print "====================="; 21 | 22 | CONVFMT="%d"; 23 | print "---%d as CONVFMT---"; 24 | printf "%s\n", A; 25 | print "====================="; 26 | } 27 | -------------------------------------------------------------------------------- /Chapter03/convfmt.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | A=123.123456789 3 | print "---Default CONVFMT---"; 4 | printf "%s\n", A; 5 | print "====================="; 6 | 7 | CONVFMT="%.4g"; 8 | print "---%.4g as CONVFMT---"; 9 | printf "%s\n", A; 10 | print "====================="; 11 | 12 | CONVFMT="%.8g"; 13 | print "---%.8g as CONVFMT---"; 14 | printf "%s\n", A; 15 | print "====================="; 16 | 17 | CONVFMT="%2.2f"; 18 | print "---%.2.2f as CONVFMT---"; 19 | printf "%s\n", A; 20 | print "====================="; 21 | 22 | CONVFMT="%d"; 23 | print "---%d as CONVFMT---"; 24 | printf "%s\n", A; 25 | print "====================="; 26 | } 27 | -------------------------------------------------------------------------------- /Chapter05/printf_redirection1.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | printf "--------------------------------------------------------------------------------\n"> "emp_report" 3 | printf "|%-6s\t|%-6s\t|%-10s\t|%-17s\t|%-3s\t|%-3s\t|%-6s|\n", 4 | "FName","LName","ContactNo.","EmailId","Sex","Dpt","Salary" > "emp_report" 5 | printf "--------------------------------------------------------------------------------\n"> "emp_report" 6 | } 7 | { 8 | printf "|%-6s\t|%-6s\t|%-10d\t|%-17s\t|%-3s\t|%-3s\t|%-6d|\n", $1,$2,$3,$4,$5,$6,$7> "emp_report" 9 | } 10 | END { 11 | printf "--------------------------------------------------------------------------------\n"> "emp_report" 12 | } 13 | -------------------------------------------------------------------------------- /Chapter01/cars.dat: -------------------------------------------------------------------------------- 1 | maruti swift 2007 50000 5 2 | honda city 2005 60000 3 3 | maruti dezire 2009 3100 6 4 | chevy beat 2005 33000 2 5 | honda city 2010 33000 6 6 | chevy tavera 1999 10000 4 7 | toyota corolla 1995 95000 2 8 | maruti swift 2009 4100 5 9 | maruti esteem 1997 98000 1 10 | ford ikon 1995 80000 1 11 | honda accord 2000 60000 2 12 | fiat punto 2007 45000 3 13 | -------------------------------------------------------------------------------- /Chapter03/cars.dat: -------------------------------------------------------------------------------- 1 | maruti swift 2007 50000 5 2 | honda city 2005 60000 3 3 | maruti dezire 2009 3100 6 4 | chevy beat 2005 33000 2 5 | honda city 2010 33000 6 6 | chevy tavera 1999 10000 4 7 | toyota corolla 1995 95000 2 8 | maruti swift 2009 4100 5 9 | maruti esteem 1997 98000 1 10 | ford ikon 1995 80000 1 11 | honda accord 2000 60000 2 12 | fiat punto 2007 45000 3 13 | -------------------------------------------------------------------------------- /Chapter04/cars.dat: -------------------------------------------------------------------------------- 1 | maruti swift 2007 50000 5 2 | honda city 2005 60000 3 3 | maruti dezire 2009 3100 6 4 | chevy beat 2005 33000 2 5 | honda city 2010 33000 6 6 | chevy tavera 1999 10000 4 7 | toyota corolla 1995 95000 2 8 | maruti swift 2009 4100 5 9 | maruti esteem 1997 98000 1 10 | ford ikon 1995 80000 1 11 | honda accord 2000 60000 2 12 | fiat punto 2007 45000 3 13 | -------------------------------------------------------------------------------- /Chapter05/cars.dat: -------------------------------------------------------------------------------- 1 | maruti swift 2007 50000 5 2 | honda city 2005 60000 3 3 | maruti dezire 2009 3100 6 4 | chevy beat 2005 33000 2 5 | honda city 2010 33000 6 6 | chevy tavera 1999 10000 4 7 | toyota corolla 1995 95000 2 8 | maruti swift 2009 4100 5 9 | maruti esteem 1997 98000 1 10 | ford ikon 1995 80000 1 11 | honda accord 2000 60000 2 12 | fiat punto 2007 45000 3 13 | -------------------------------------------------------------------------------- /Chapter06/cars.dat: -------------------------------------------------------------------------------- 1 | maruti swift 2007 50000 5 2 | honda city 2005 60000 3 3 | maruti dezire 2009 3100 6 4 | chevy beat 2005 33000 2 5 | honda city 2010 33000 6 6 | chevy tavera 1999 10000 4 7 | toyota corolla 1995 95000 2 8 | maruti swift 2009 4100 5 9 | maruti esteem 1997 98000 1 10 | ford ikon 1995 80000 1 11 | honda accord 2000 60000 2 12 | fiat punto 2007 45000 3 13 | -------------------------------------------------------------------------------- /Chapter07/cars.dat: -------------------------------------------------------------------------------- 1 | maruti swift 2007 50000 5 2 | honda city 2005 60000 3 3 | maruti dezire 2009 3100 6 4 | chevy beat 2005 33000 2 5 | honda city 2010 33000 6 6 | chevy tavera 1999 10000 4 7 | toyota corolla 1995 95000 2 8 | maruti swift 2009 4100 5 9 | maruti esteem 1997 98000 1 10 | ford ikon 1995 80000 1 11 | honda accord 2000 60000 2 12 | fiat punto 2007 45000 3 13 | -------------------------------------------------------------------------------- /Chapter08/cars.dat: -------------------------------------------------------------------------------- 1 | maruti swift 2007 50000 5 2 | honda city 2005 60000 3 3 | maruti dezire 2009 3100 6 4 | chevy beat 2005 33000 2 5 | honda city 2010 33000 6 6 | chevy tavera 1999 10000 4 7 | toyota corolla 1995 95000 2 8 | maruti swift 2009 4100 5 9 | maruti esteem 1997 98000 1 10 | ford ikon 1995 80000 1 11 | honda accord 2000 60000 2 12 | fiat punto 2007 45000 3 13 | -------------------------------------------------------------------------------- /Chapter09/cars.dat: -------------------------------------------------------------------------------- 1 | maruti swift 2007 50000 5 2 | honda city 2005 60000 3 3 | maruti dezire 2009 3100 6 4 | chevy beat 2005 33000 2 5 | honda city 2010 33000 6 6 | chevy tavera 1999 10000 4 7 | toyota corolla 1995 95000 2 8 | maruti swift 2009 4100 5 9 | maruti esteem 1997 98000 1 10 | ford ikon 1995 80000 1 11 | honda accord 2000 60000 2 12 | fiat punto 2007 45000 3 13 | -------------------------------------------------------------------------------- /Chapter10/cars.dat: -------------------------------------------------------------------------------- 1 | maruti swift 2007 50000 5 2 | honda city 2005 60000 3 3 | maruti dezire 2009 3100 6 4 | chevy beat 2005 33000 2 5 | honda city 2010 33000 6 6 | chevy tavera 1999 10000 4 7 | toyota corolla 1995 95000 2 8 | maruti swift 2009 4100 5 9 | maruti esteem 1997 98000 1 10 | ford ikon 1995 80000 1 11 | honda accord 2000 60000 2 12 | fiat punto 2007 45000 3 13 | -------------------------------------------------------------------------------- /Chapter08/func1.awk: -------------------------------------------------------------------------------- 1 | function find_add(num1, num2){ 2 | result = num1 + num2 3 | printf ("Addition of %d + %d : %d\n", num1,num2,result) 4 | } 5 | 6 | function find_sub(num1, num2){ 7 | result = num1 - num2 8 | printf ("Subtraction of %d - %d : %d\n", num1,num2,result) 9 | } 10 | 11 | function find_mul(num1, num2){ 12 | result = num1 * num2 13 | printf ("Multiplication of %d * %d : %d\n", num1,num2,result) 14 | } 15 | 16 | # Main function 17 | function main(){ 18 | printf "Enter first number : " 19 | getline num1 < "-" 20 | printf "Enter second number : " 21 | getline num2 < "-" 22 | find_add(num1,num2) 23 | find_sub(num1,num2) 24 | find_mul(num1,num2) 25 | } 26 | 27 | 28 | BEGIN { 29 | 30 | main() 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter10/array2html.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | FS =":"; 3 | printf "%s%s%s", 4 | "\n"; 7 | 8 | } 9 | 10 | (NR==1){ 11 | printf " \n" 12 | for( i=1; i<=NF; i++ ) 13 | { 14 | printf " \n", $i; 15 | } 16 | printf " \n" 17 | } 18 | 19 | (NF>0 && NR>1){ 20 | 21 | printf " \n" 22 | for( i=1; i<=NF; i++ ) 23 | { 24 | if ( i==1 ) { 25 | printf " \n", $i; 26 | } else { 27 | printf " \n", $i; 28 | } 29 | } 30 | printf " \n" 31 | } 32 | 33 | END { 34 | printf "
%s
%s%s
\n"; 35 | } 36 | -------------------------------------------------------------------------------- /Chapter08/func3.awk: -------------------------------------------------------------------------------- 1 | function find_add(num1, num2){ 2 | result = num1 + num2 3 | return result 4 | } 5 | 6 | function find_sub(num1, num2){ 7 | result = num1 - num2 8 | return result 9 | } 10 | 11 | function find_mul(num1, num2){ 12 | result = num1 * num2 13 | return result 14 | } 15 | 16 | # Main function 17 | function main(){ 18 | printf "Enter first number : " 19 | getline num1 < "-" 20 | printf "Enter second number : " 21 | getline num2 < "-" 22 | add = find_add(num1,num2) 23 | print "Addition of above num : ", add 24 | subtraction = find_sub(num1,num2) 25 | print "Subtraction of above num : ", subtraction 26 | mult = find_mul(num1,num2) 27 | print "Multiplication of above num : ", mult 28 | } 29 | 30 | 31 | BEGIN { 32 | 33 | main() 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Chapter09/asorti_2.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | arr[30] = "volvo" 3 | arr[10] = "bmw" 4 | arr[20] = "audi" 5 | arr[50] = "toyota" 6 | arr["car"] = "ferrari" 7 | arr[70] = "renault" 8 | arr[110] = 20 9 | arr[40] = "ford" 10 | arr["num"] = 10 11 | arr[80] = "porsche" 12 | arr[60] = "jeep" 13 | 14 | n = asorti(arr, newarr) 15 | print "============================================" 16 | print "SORTED ARRAY INDEXES STORED AS ELEMENTS ..." 17 | print "============================================" 18 | for ( x in newarr ) 19 | print "Index : ", x, "\tValue :", newarr[x] 20 | 21 | print "============================================" 22 | print "ORIGINAL ARRAY...." 23 | print "============================================" 24 | for ( v in arr ) 25 | print "Index : ", v, "\tValue :", arr[v] 26 | } 27 | -------------------------------------------------------------------------------- /Chapter09/asort_newarr.awk: -------------------------------------------------------------------------------- 1 | BEGIN { 2 | arr[30] = "volvo" 3 | arr[10] = "bmw" 4 | arr[20] = "audi" 5 | arr[50] = "toyota" 6 | arr["car"] = "ferrari" 7 | arr[70] = "renault" 8 | arr[110] = 20 9 | arr[40] = "ford" 10 | arr["num"] = 10 11 | arr[80] = "porsche" 12 | arr[60] = "jeep" 13 | 14 | n = asort(arr, newarr) 15 | print "============================================" 16 | print "SORTED ARRAY STORED IN NEW ARRAY VARIABLE..." 17 | print "============================================" 18 | for ( x in newarr ) 19 | print "Index : ", x, "\tValue :", newarr[x] 20 | 21 | print "============================================" 22 | print "ORIGINAL ARRAY...." 23 | print "============================================" 24 | for ( v in arr ) 25 | print "Index : ", v, "\tValue :", arr[v] 26 | } 27 | -------------------------------------------------------------------------------- /Chapter09/num.txt: -------------------------------------------------------------------------------- 1 | 50 0x32 062 2 | 49 0x31 061 3 | 48 0x30 060 4 | 47 0x2F 057 5 | 46 0x2E 056 6 | 45 0x2D 055 7 | 44 0x2C 054 8 | 43 0x2B 053 9 | 42 0x2A 052 10 | 41 0x29 051 11 | 40 0x28 050 12 | 39 0x27 047 13 | 38 0x26 046 14 | 37 0x25 045 15 | 36 0x24 044 16 | 35 0x23 043 17 | 34 0x22 042 18 | 33 0x21 041 19 | 32 0x20 040 20 | 31 0x1F 037 21 | 30 0x1E 036 22 | 29 0x1D 035 23 | 28 0x1C 034 24 | 27 0x1B 033 25 | 26 0x1A 032 26 | 25 0x19 031 27 | 24 0x18 030 28 | 23 0x17 027 29 | 22 0x16 026 30 | 21 0x15 025 31 | 20 0x14 024 32 | 19 0x13 023 33 | 18 0x12 022 34 | 17 0x11 021 35 | 16 0x10 020 36 | 15 0x0F 017 37 | 14 0x0E 016 38 | 13 0x0D 015 39 | 12 0x0C 014 40 | 11 0x0B 013 41 | 10 0x0A 012 42 | 9 0x09 011 43 | 8 0x08 010 44 | 7 0x07 07 45 | 6 0x06 06 46 | 5 0x05 05 47 | 4 0x04 04 48 | 3 0x03 03 49 | 2 0x02 02 50 | 1 0x01 01 51 | 0 0x0 00 52 | -------------------------------------------------------------------------------- /Chapter04/marks_summary.awk: -------------------------------------------------------------------------------- 1 | BEGIN { OFS = "\t" 2 | print "===================="; 3 | printf "Name\tAvg\tGrade\n"; 4 | print "===================="; 5 | } 6 | { 7 | student_total = 0 8 | for ( x=2; x <=NF; x++ ) 9 | student_total += $x 10 | # calculate average 11 | avg = student_total / (NF -1) 12 | student_avg[NR] = avg 13 | # determine grade of student 14 | if ( avg >= 90 ) grade = "Excellent" 15 | else if ( avg >=80 ) grade = "Very Good" 16 | else if ( avg >=70 ) grade = "Good" 17 | else if ( avg >=60 ) grade = "Satisfactory" 18 | else grade = "Fail" 19 | print $1, avg, grade 20 | } 21 | END { 22 | # calculate the average of marks scored by whole class 23 | for ( x =1; x <= NR; x++ ) 24 | class_avg_total += student_avg[x] 25 | class_avg = class_avg_total / NR 26 | print "===================="; 27 | print "Class Average: ", class_avg 28 | } 29 | -------------------------------------------------------------------------------- /Chapter01/emp.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Jane Kaur 9837432312 jane@gmail.com F hr 1800 3 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Julie Kapur 8826234556 julie@yahoo.com F Ops 2500 6 | Ana Khanna 9856422312 anak@hotmail.com F Ops 2700 7 | Hari Singh 8827255666 hari@yahoo.com M Ops 2350 8 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 9 | John Kapur 9911556789 john@gmail.com M hr 2200 10 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 11 | Sam khanna 8856345512 sam@hotmail.com F lgs 2300 12 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 13 | Emily Kaur 8826175812 emily@gmail.com F Ops 2100 14 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 15 | Vina Singh 8811776612 vina@yahoo.com F lgs 2300 16 | -------------------------------------------------------------------------------- /Chapter03/emp.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Jane Kaur 9837432312 jane@gmail.com F hr 1800 3 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Julie Kapur 8826234556 julie@yahoo.com F Ops 2500 6 | Ana Khanna 9856422312 anak@hotmail.com F Ops 2700 7 | Hari Singh 8827255666 hari@yahoo.com M Ops 2350 8 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 9 | John Kapur 9911556789 john@gmail.com M hr 2200 10 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 11 | Sam khanna 8856345512 sam@hotmail.com F lgs 2300 12 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 13 | Emily Kaur 8826175812 emily@gmail.com F Ops 2100 14 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 15 | Vina Singh 8811776612 vina@yahoo.com F lgs 2300 16 | -------------------------------------------------------------------------------- /Chapter04/emp.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Jane Kaur 9837432312 jane@gmail.com F hr 1800 3 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Julie Kapur 8826234556 julie@yahoo.com F Ops 2500 6 | Ana Khanna 9856422312 anak@hotmail.com F Ops 2700 7 | Hari Singh 8827255666 hari@yahoo.com M Ops 2350 8 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 9 | John Kapur 9911556789 john@gmail.com M hr 2200 10 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 11 | Sam khanna 8856345512 sam@hotmail.com F lgs 2300 12 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 13 | Emily Kaur 8826175812 emily@gmail.com F Ops 2100 14 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 15 | Vina Singh 8811776612 vina@yahoo.com F lgs 2300 16 | -------------------------------------------------------------------------------- /Chapter05/emp.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Jane Kaur 9837432312 jane@gmail.com F hr 1800 3 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Julie Kapur 8826234556 julie@yahoo.com F Ops 2500 6 | Ana Khanna 9856422312 anak@hotmail.com F Ops 2700 7 | Hari Singh 8827255666 hari@yahoo.com M Ops 2350 8 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 9 | John Kapur 9911556789 john@gmail.com M hr 2200 10 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 11 | Sam khanna 8856345512 sam@hotmail.com F lgs 2300 12 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 13 | Emily Kaur 8826175812 emily@gmail.com F Ops 2100 14 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 15 | Vina Singh 8811776612 vina@yahoo.com F lgs 2300 16 | -------------------------------------------------------------------------------- /Chapter06/emp.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Jane Kaur 9837432312 jane@gmail.com F hr 1800 3 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Julie Kapur 8826234556 julie@yahoo.com F Ops 2500 6 | Ana Khanna 9856422312 anak@hotmail.com F Ops 2700 7 | Hari Singh 8827255666 hari@yahoo.com M Ops 2350 8 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 9 | John Kapur 9911556789 john@gmail.com M hr 2200 10 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 11 | Sam khanna 8856345512 sam@hotmail.com F lgs 2300 12 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 13 | Emily Kaur 8826175812 emily@gmail.com F Ops 2100 14 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 15 | Vina Singh 8811776612 vina@yahoo.com F lgs 2300 16 | -------------------------------------------------------------------------------- /Chapter07/emp.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Jane Kaur 9837432312 jane@gmail.com F hr 1800 3 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Julie Kapur 8826234556 julie@yahoo.com F Ops 2500 6 | Ana Khanna 9856422312 anak@hotmail.com F Ops 2700 7 | Hari Singh 8827255666 hari@yahoo.com M Ops 2350 8 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 9 | John Kapur 9911556789 john@gmail.com M hr 2200 10 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 11 | Sam khanna 8856345512 sam@hotmail.com F lgs 2300 12 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 13 | Emily Kaur 8826175812 emily@gmail.com F Ops 2100 14 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 15 | Vina Singh 8811776612 vina@yahoo.com F lgs 2300 16 | -------------------------------------------------------------------------------- /Chapter08/emp.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Jane Kaur 9837432312 jane@gmail.com F hr 1800 3 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Julie Kapur 8826234556 julie@yahoo.com F Ops 2500 6 | Ana Khanna 9856422312 anak@hotmail.com F Ops 2700 7 | Hari Singh 8827255666 hari@yahoo.com M Ops 2350 8 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 9 | John Kapur 9911556789 john@gmail.com M hr 2200 10 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 11 | Sam khanna 8856345512 sam@hotmail.com F lgs 2300 12 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 13 | Emily Kaur 8826175812 emily@gmail.com F Ops 2100 14 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 15 | Vina Singh 8811776612 vina@yahoo.com F lgs 2300 16 | -------------------------------------------------------------------------------- /Chapter09/emp.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Jane Kaur 9837432312 jane@gmail.com F hr 1800 3 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Julie Kapur 8826234556 julie@yahoo.com F Ops 2500 6 | Ana Khanna 9856422312 anak@hotmail.com F Ops 2700 7 | Hari Singh 8827255666 hari@yahoo.com M Ops 2350 8 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 9 | John Kapur 9911556789 john@gmail.com M hr 2200 10 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 11 | Sam khanna 8856345512 sam@hotmail.com F lgs 2300 12 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 13 | Emily Kaur 8826175812 emily@gmail.com F Ops 2100 14 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 15 | Vina Singh 8811776612 vina@yahoo.com F lgs 2300 16 | -------------------------------------------------------------------------------- /Chapter10/emp.dat: -------------------------------------------------------------------------------- 1 | Jack Singh 9857532312 jack@gmail.com M hr 2000 2 | Jane Kaur 9837432312 jane@gmail.com F hr 1800 3 | Eva Chabra 8827232115 eva@gmail.com F lgs 2100 4 | Amit Sharma 9911887766 amit@yahoo.com M lgs 2350 5 | Julie Kapur 8826234556 julie@yahoo.com F Ops 2500 6 | Ana Khanna 9856422312 anak@hotmail.com F Ops 2700 7 | Hari Singh 8827255666 hari@yahoo.com M Ops 2350 8 | Victor Sharma 8826567898 vics@hotmail.com M Ops 2500 9 | John Kapur 9911556789 john@gmail.com M hr 2200 10 | Billy Chabra 9911664321 bily@yahoo.com M lgs 1900 11 | Sam khanna 8856345512 sam@hotmail.com F lgs 2300 12 | Ginny Singh 9857123466 ginny@yahoo.com F hr 2250 13 | Emily Kaur 8826175812 emily@gmail.com F Ops 2100 14 | Amy Sharma 9857536898 amys@hotmail.com F Ops 2500 15 | Vina Singh 8811776612 vina@yahoo.com F lgs 2300 16 | -------------------------------------------------------------------------------- /Chapter09/codeprof.awk: -------------------------------------------------------------------------------- 1 | func z_array(){ 2 | 3 | arr[30] = "volvo" 4 | arr[10] = "bmw" 5 | arr[20] = "audi" 6 | arr[50] = "toyota" 7 | arr["car"] = "ferrari" 8 | 9 | n = asort(arr) 10 | print "Array begins...!" 11 | print "=====================" 12 | for ( v in arr ) 13 | print v, arr[v] 14 | print "Array Ends...!" 15 | print "=====================" 16 | } 17 | 18 | function mul(num1, num2){ 19 | result = num1 * num2 20 | printf ("Multiplication of %d * %d : %d\n", num1,num2,result) 21 | } 22 | 23 | function all(){ 24 | add(30,10) 25 | mul(5,6) 26 | z_array() 27 | } 28 | 29 | 30 | BEGIN { print "First BEGIN statement" 31 | print "=====================" 32 | } 33 | END { print "First END statement " 34 | print "=====================" 35 | } 36 | 37 | /maruti/{print $0 } 38 | 39 | 40 | BEGIN { 41 | print "Second BEGIN statement" 42 | print "=====================" 43 | all() 44 | } 45 | END { print "Second END statement" 46 | print "=====================" 47 | } 48 | 49 | function add(num1, num2){ 50 | result = num1 + num2 51 | printf ("Addition of %d + %d : %d\n", num1,num2,result) 52 | } 53 | -------------------------------------------------------------------------------- /Chapter09/user_functions.awk: -------------------------------------------------------------------------------- 1 | # Returns minimum number 2 | function find_min(num1, num2){ 3 | if (num1 < num2) 4 | return num1 5 | else 6 | return num2 7 | } 8 | 9 | # Returns maximum number 10 | function find_max(num1, num2){ 11 | if (num1 > num2) 12 | return num1 13 | else 14 | return num2 15 | } 16 | 17 | # Main function 18 | function minmax(num1, num2){ 19 | 20 | # Find minimum number 21 | result = find_min(10, 20) 22 | print "Minimum =", result 23 | 24 | # Find maximum number 25 | result = find_max(10, 20) 26 | print "Maximum =", result 27 | } 28 | # Add 2 numbers 29 | function add(num1, num2){ 30 | result = num1 + num2 31 | print result 32 | } 33 | 34 | function sortarr(){ 35 | 36 | arr[30] = "volvo" 37 | arr[10] = "bmw" 38 | arr[20] = "audi" 39 | arr[50] = "toyota" 40 | arr["car"] = "ferrari" 41 | arr[70] = "renault" 42 | arr[110] = 20 43 | arr[40] = "ford" 44 | arr["num"] = 10 45 | arr[80] = "porsche" 46 | arr[60] = "jeep" 47 | 48 | n = asorti(arr) 49 | for ( v in arr ) 50 | print v, arr[v] 51 | } 52 | 53 | BEGIN { 54 | 55 | add(20,5) 56 | sortarr() 57 | minmax(10, 20) 58 | 59 | } 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Chapter09/awkprof.out: -------------------------------------------------------------------------------- 1 | # gawk profile, created Fri Feb 9 11:04:19 2018 2 | 3 | # BEGIN rule(s) 4 | 5 | BEGIN { 6 | print "First BEGIN statement" 7 | print "=====================" 8 | } 9 | 10 | BEGIN { 11 | print "Second BEGIN statement" 12 | print "=====================" 13 | all() 14 | } 15 | 16 | # Rule(s) 17 | 18 | /maruti/ { 19 | print $0 20 | } 21 | 22 | # END rule(s) 23 | 24 | END { 25 | print "First END statement " 26 | print "=====================" 27 | } 28 | 29 | END { 30 | print "Second END statement" 31 | print "=====================" 32 | } 33 | 34 | 35 | # Functions, listed alphabetically 36 | 37 | function add(num1, num2) 38 | { 39 | result = num1 + num2 40 | printf "Addition of %d + %d : %d\n", num1, num2, result 41 | } 42 | 43 | function all() 44 | { 45 | add(30, 10) 46 | mul(5, 6) 47 | z_array() 48 | } 49 | 50 | function mul(num1, num2) 51 | { 52 | result = num1 * num2 53 | printf "Multiplication of %d * %d : %d\n", num1, num2, result 54 | } 55 | 56 | function z_array() 57 | { 58 | arr[30] = "volvo" 59 | arr[10] = "bmw" 60 | arr[20] = "audi" 61 | arr[50] = "toyota" 62 | arr["car"] = "ferrari" 63 | n = asort(arr) 64 | print "Array begins...!" 65 | print "=====================" 66 | for (v in arr) { 67 | print v, arr[v] 68 | } 69 | print "Array Ends...!" 70 | print "=====================" 71 | } 72 | -------------------------------------------------------------------------------- /Chapter05/emp_report: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | |FName |LName |ContactNo. |EmailId |Sex |Dpt |Salary| 3 | -------------------------------------------------------------------------------- 4 | |Jack |Singh |9857532312 |jack@gmail.com |M |hr |2000 | 5 | |Jane |Kaur |9837432312 |jane@gmail.com |F |hr |1800 | 6 | |Eva |Chabra |8827232115 |eva@gmail.com |F |lgs |2100 | 7 | |Amit |Sharma |9911887766 |amit@yahoo.com |M |lgs |2350 | 8 | |Julie |Kapur |8826234556 |julie@yahoo.com |F |Ops |2500 | 9 | |Ana |Khanna |9856422312 |anak@hotmail.com |F |Ops |2700 | 10 | |Hari |Singh |8827255666 |hari@yahoo.com |M |Ops |2350 | 11 | |Victor |Sharma |8826567898 |vics@hotmail.com |M |Ops |2500 | 12 | |John |Kapur |9911556789 |john@gmail.com |M |hr |2200 | 13 | |Billy |Chabra |9911664321 |bily@yahoo.com |M |lgs |1900 | 14 | |Sam |khanna |8856345512 |sam@hotmail.com |F |lgs |2300 | 15 | |Ginny |Singh |9857123466 |ginny@yahoo.com |F |hr |2250 | 16 | |Emily |Kaur |8826175812 |emily@gmail.com |F |Ops |2100 | 17 | |Amy |Sharma |9857536898 |amys@hotmail.com |F |Ops |2500 | 18 | |Vina |Singh |8811776612 |vina@yahoo.com |F |lgs |2300 | 19 | -------------------------------------------------------------------------------- 20 | =========employee database ends here============= 21 | -------------------------------------------------------------------------------- /Chapter09/codeprof.prof: -------------------------------------------------------------------------------- 1 | # gawk profile, created Fri Feb 9 11:01:41 2018 2 | 3 | # BEGIN rule(s) 4 | 5 | BEGIN { 6 | 1 print "First BEGIN statement" 7 | 1 print "=====================" 8 | } 9 | 10 | BEGIN { 11 | 1 print "Second BEGIN statement" 12 | 1 print "=====================" 13 | 1 all() 14 | } 15 | 16 | # Rule(s) 17 | 18 | 12 /maruti/ { # 4 19 | 4 print $0 20 | } 21 | 22 | # END rule(s) 23 | 24 | END { 25 | 1 print "First END statement " 26 | 1 print "=====================" 27 | } 28 | 29 | END { 30 | 1 print "Second END statement" 31 | 1 print "=====================" 32 | } 33 | 34 | 35 | # Functions, listed alphabetically 36 | 37 | 1 function add(num1, num2) 38 | { 39 | 1 result = num1 + num2 40 | 1 printf "Addition of %d + %d : %d\n", num1, num2, result 41 | } 42 | 43 | 1 function all() 44 | { 45 | 1 add(30, 10) 46 | 1 mul(5, 6) 47 | 1 z_array() 48 | } 49 | 50 | 1 function mul(num1, num2) 51 | { 52 | 1 result = num1 * num2 53 | 1 printf "Multiplication of %d * %d : %d\n", num1, num2, result 54 | } 55 | 56 | 1 function z_array() 57 | { 58 | 1 arr[30] = "volvo" 59 | 1 arr[10] = "bmw" 60 | 1 arr[20] = "audi" 61 | 1 arr[50] = "toyota" 62 | 1 arr["car"] = "ferrari" 63 | 1 n = asort(arr) 64 | 1 print "Array begins...!" 65 | 1 print "=====================" 66 | 5 for (v in arr) { 67 | 5 print v, arr[v] 68 | } 69 | 1 print "Array Ends...!" 70 | 1 print "=====================" 71 | } 72 | -------------------------------------------------------------------------------- /Chapter10/http_report.awk: -------------------------------------------------------------------------------- 1 | #Web server log file analysis & filtering 2 | 3 | BEGIN{ 4 | FS="\"" 5 | } 6 | 7 | { 8 | split($1, a, " ") 9 | ip=a[1] 10 | if($2!="") 11 | { 12 | datetime=a[4]" "a[5] 13 | request=$2 14 | referer=$4 15 | useragent=$6 16 | split($3, c, " ") 17 | code=c[1] 18 | size=c[2] 19 | } 20 | else 21 | { 22 | split($3, b, " ") 23 | datetime=b[2]" "b[3] 24 | request=$4 25 | referer=$6 26 | useragent=$8 27 | split($5, c, " ") 28 | code=c[1] 29 | size=c[2] 30 | } 31 | total=NR 32 | if(match(code, /^[0-9]+$/)==0) 33 | { 34 | code="UNKNOWN" 35 | } 36 | statuses[code]++ 37 | 38 | # Analyse the request 39 | n=split(request, detail, " ") 40 | method=detail[1] 41 | if(match(method, /^[A-Z]+$/)==0) 42 | { 43 | method="UNKNOWN" 44 | } 45 | methods[method]++ 46 | 47 | url="" 48 | for(i=2; iIf you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF. 41 |

https://packt.link/free-ebook/9781788391030

--------------------------------------------------------------------------------