AWK FUNCTIONS
AWK STRING SUBSTITUTION1 Replaces first match of regexp with replstring
sub(regexp,replstring,mystring)
AWK STRING SUBSTITUTION2 Replaces ALL matches of regexp with replstring
gsub(/o/,"O",mystring)
AWK SPLIT FUNCTION Chop up string and place values into integer indexed array
numelements=split("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec",mymonths,",")
$ awk 'BEGIN{ORS="=";} {print;}' student-marks
The output record separator is the equivalent of the record separator, but
describes the delimiter only in the output of the awk script.
$cat student.awk
BEGIN {
RS="\n\n";
FS="\n";
}
{
print $1,$2;
}
The record separator describes how the awk output hs its records
separated, the default is newline but anything can be set.
PASSING SHELL VARIABLES INTO AWK #!/bin/bash
echo -n "Enter hour (0-23): "
read hour
date | awk -v awk_hr=$hour 'BEGIN { FS="[ :]+"; } ; $4 >= awk_hr { print $2, $3, $8 }'
$ awk '{print "Processing Record - ",NR;}END {print NR, "Students Records are processed";}' student-marks
Output : Processing Record - 1
Processing Record - 2
Processing Record - 3
Processing Record - 4
Processing Record - 5
5 Students Records are processed
AWK + MULTILINE RECORDS # Records are separated by blank lines.
# Each line is one field.
BEGIN { RS = "" ; FS = "\n" }
{
print "Name is:", $1
}
AWK MATCH FUNCTION
Returns the relative position of the regexp, starting digit and length
print match(mystring,/you/), RSTART, RLENGTH
do {
print "I get printed
} while ( count != 1 )
for ( initial assignment; comparison; increment ) {
code block
}
while(1) {
print "iteration",x
if ( x == 10 ) {
break
}
x++
}
* We can always use continue instaed
#Function : Returns the length of a found string
print length(mystring) OR print length($var)
Function : Returns the relative position (index) of the search word
print index(mystring)
$ awk '{print FILENAME, FNR;}' student-marks bookdetails
student-marks 1
student-marks 2
student-marks 3
student-marks 4
student-marks 5
bookdetails 1
bookdetails 2
bookdetails 3
bookdetails 4
bookdetails 5
$ awk '{print FILENAME}' student-marks
student-marks
student-marks
student-marks
student-marks
student-marks
$ awk -F 'FS' 'commands' inputfilename Sample code :
OR
$ awk 'BEGIN{FS="FS";}'
The field separator default value is space ' '
The field separator can be set to any other delimiter
by using the above options
############
$ cat etc_passwd.awk
BEGIN{
FS=":";
print "Name\tUserID\tGroupID\tHomeDirectory";
}
{
print $1"\t"$3"\t"$4"\t"$6;
}
END {
print NR,"Records Processed";
CALL AWK FROM SCRIPT
awk -f myscript.awk myfile.in
chmod +x myscript.awk and ./myscript.awk myfile.in
AWK SETTING DELIMITERS
awk -F":" '{ print $1 }' /etc/passwd
BEGIN {
FS=":"
}
{ print $1 }
awk -f myscript.awk myfile.in
chmod +x myscript.awk and ./myscript.awk myfile.in
AWK AND CONDITIONAL EXPRESSIONS { ! /matchme/ { print $1 $3 $4 } is equivalent to
if ( $5 ~ /root/ ) { {
print $3 if ( $0 !~ /matchme/ ) {
} print $1 $3 $4
Boolean Operators : }
( $1 == "foo" ) && ( $2 == "bar" ) { print } }
1. awksubstring
2. awklength
3. awkindex
4. awktolower
5. awktoupper
6. awkmatch
7. awkstrsub
8. awksplit
9. awkdelimit
10. awkpassingvars
11. callawkfromscript
12. awkexecutable
13. awkmultiline
13. awkconditionals
13. awkloops
14. awkfieldseparators
15. awkrecordseparators
16. awkrecordseparators(out)
17. awkfieldseparators(out)
18. awknumberofrecordsvar
19. awknumberoffieldsvar
Converts all lowercase to upper
print toupper(mystring)
Converts all upper case to lower case
print tolower(mystring)
awk '{print substr($1,3) }' temp
substr(mystring,startpos,maxlen)
'a[substr($0,135,1)]++{exit 1}' test.txt
if(substr($0,750,1)=="1"
if(substr($rec1,750,1)=="X" || substr($rec2,750,1)=="X"
print substr(mystring,9,3)
$ awk '{print NR,"->",NF}' student-marks
1 -> 5
2 -> 5
3 -> 4
4 -> 5
5 -> 4
Input : BEGIN { Output : Hello there Jim!
FS="\n" Sample Code :
RS=""
OFS=", " $ awk -F':' 'BEGIN{OFS="=";} {print $3,$4;}' /etc/passwd
}
{
print $1, $2, $3
}
The OFS describes the kind of delimiter than will be used
in the output of the awk script.