What is AWK?
AWK is a programming language designed for processing text-based data, either in files or data streams. It is an example of a programming language that extensively uses the string datatype, associative arrays , and regular expressions.
AWK is a programming language designed for processing text-based data, either in files or data streams. It is an example of a programming language that extensively uses the string datatype, associative arrays , and regular expressions.
There are three variations of AWK:
AWK - the original from AT&T
NAWK - A newer, improved version from AT&T
GAWK - The Free Software foundation's version
NAWK - A newer, improved version from AT&T
GAWK - The Free Software foundation's version
AWK is an excellent filter and report writer.AWK also has string manipulation functions, so it can search for particular strings and modify the output.
The essential organization of an AWK program follows the form:
pattern { action }
The pattern specifies when the action is performed.i.e the pattern specifies a test that is performed with each line read as input. If the condition is true, then the action is taken.
Lets look some examples,First of all create a file named "file1.txt", and write somthing like "unix tool awk " in the body and then save,then type the following command in terminal
$awk '{print $1}' file1.txt
The output will be 'unix', i.e it will print the first word in the body of the file.
Two other important patterns can be specified by the keywords "BEGIN" and "END.",these two words specify actions to be taken before any lines are read.and after the last line is read.THe AWK program below:
BEGIN ( print "START" )
( print )
END ( print "STOP" )
We can make this into a typical AWK program :
Firt of create two files like "sample1.txt "with body " sam 15 groupA" & "sample2.txt" with body "neil 13 groupB" ,then write our AWK program as described below and save the file with extention (.awk) in the same directory that your sample file resides ,Here my awk program will be "file1.awk"
AWK is also an interpreter,Here we making the file executable by adding the first line in the beginning of the file i.e (" #!/bin/awk -f ").
programe name : file1.awk
Then change the permission with the chmod command (i.e "chmod +x file1.awk" ),and then the script becomes a new command.Here the "-f" option following "#!/bin/awk" make the file execute directly i.e " awk -f file1.awk".
Lets look some examples,First of all create a file named "file1.txt", and write somthing like "unix tool awk " in the body and then save,then type the following command in terminal
$awk '{print $1}' file1.txt
The output will be 'unix', i.e it will print the first word in the body of the file.
Two other important patterns can be specified by the keywords "BEGIN" and "END.",these two words specify actions to be taken before any lines are read.and after the last line is read.THe AWK program below:
BEGIN ( print "START" )
( print )
END ( print "STOP" )
We can make this into a typical AWK program :
Firt of create two files like "sample1.txt "with body " sam 15 groupA" & "sample2.txt" with body "neil 13 groupB" ,then write our AWK program as described below and save the file with extention (.awk) in the same directory that your sample file resides ,Here my awk program will be "file1.awk"
AWK is also an interpreter,Here we making the file executable by adding the first line in the beginning of the file i.e (" #!/bin/awk -f ").
programe name : file1.awk
#!/bin/awk -f BEGIN { print "Name\tAge\tGroup" } { print $1, "\t" ,$2, "\t" ,$3 } END { print " - DONE -" }
Then change the permission with the chmod command (i.e "chmod +x file1.awk" ),and then the script becomes a new command.Here the "-f" option following "#!/bin/awk" make the file execute directly i.e " awk -f file1.awk".
Then run the file by typing "$ awk -f file1.awk sample1.txt sample2.txt" in your terminal.then you will get the following output.
This is just a brief introduction to on AWK,You can find more about AWK by using the following document. AwK-Tutorial
sreehari@~/sed awk make/awk09:54:02$ $awk -f file1.awk sample1.txt sample2.txt Name Age Group sam 15 groupA neil 13 groupB - DONE -
This is just a brief introduction to on AWK,You can find more about AWK by using the following document. AwK-Tutorial
No comments:
Post a Comment
Note: only a member of this blog may post a comment.