Detail video : • Unix Basics Part 12 | ...
AWK -
awk is a domain-specific language used for text processing and typically used as a data extraction generate reports based on requirements.
Like sed and grep, it is a filter, and available on most of the Unix versions.
It was created at Bell Labs in the 1970s, its name is derived from the authors:
Alfred Aho
Peter Weinberger
Brian Kernighan
awk scans a file line by line.
It can split each input line into fields.
It compares input line/fields to pattern.
We can preform action on match pattern or lines.
Command:
awk options 'selection _criteria {action }' input-file
-f program-file : Reads the AWK program source from the file
program-file, instead of from the
first command line argument.
-F fs : Use fs for the input field separator
Built-In Variables In Awk
NR: NR command keeps a current count of the number of input records, like lines.( Number Records)
NF: NF command keeps a count of the number of fields within the current input record.( Number Fields)
FS: FS command contains the field separator character which is used to divide fields on the input line, default is white space, e.g ";" ,"," etc.(Field Separator)
OFS: OFS command stores the output field separator, which separates the fields when Awk prints them, default is a blank space.(Output Field Separator)
RS: RS command stores the current record separator character, default is new line char.(Records Separator)
ORS: ORS command stores the output record separator, default is new line char.
1) Display all lines.
$ awk '{print}' test_awk.dat / $ awk '{print $0}' test_awk.dat
2) Display Line Number along with line.
$ awk '{print NR,$0}' test_awk.dat
3) Display Line From 3 to 6.
$ awk 'NR==3, NR==6 {print NR,$0}' test_awk.dat
4) To print the line with the row number(NR) separated with ” - “.
$ awk '{print NR " - " $1 }' test_awk.dat
5) Display First and Last Field
$ awk '{print $1,$NF}' test_awk.dat
6) Display all lines matching the pattern.
$ awk ' /Mark/ {print}' test_awk.dat
Using BEGIN /END - Normally awk command works when it reads the all-input lines(like print), sometimes we need to perform action before the actual command acts.
A BEGIN rule is executed once only, before the first input record is read.
END rule is executed once only, after all the input is read.
7) Using FS to display fields.
awk -F',' '{print $3,$4;}' test_awk.csv
or
awk 'BEGIN { FS = "," } ; { print $3,$4 } ' test_awk.csv
8) Using OFS.
awk -F',' 'BEGIN{OFS=":";} {print $3,$4;}' test_awk.csv
9) Using RS.
$cat test_awk_file.awk
BEGIN {
FS=“,";
RS="
";
OFS="==";
ORS ="
----------------------
";
}
{
print $1,$2;
}
$ awk -f test_awk_file.awk test_awk.dat
10) There are functions to perform mathematical and other operations.
length − This function is used to determine length of a string.
substr − This function is used to extract a substring from a string.
split − This function is used to split a string into an array based on a specified delimiter.
toupper - This function is used to convert a string to upper case.
tolower - This function is used to convert a string to lower case.
sqrt - This function is used to calculate square root of a number.
e.g. :
$ awk 'BEGIN { print sqrt(16); } '
$ awk 'BEGIN{print length("technocoders")} '
$ awk 'BEGIN{print substr("Let’s learn programming", 7)} '
$ awk 'BEGIN{print substr("Let’s learn programming", 7, 5)}'
$ awk 'BEGIN{print tolower("TECHNOCODERS")} '
$ awk 'BEGIN{print toupper("technocoders ")}'
$awk 'BEGIN{
l_var=" Let’s learn programming ";
l_field=" ";
l_value=split(l_var, array, l_field);
-- loop operation here
{printf("%s
", array[i]);}
} '
10) How to do comparison and mathematical calculations.
To print the squares of first numbers from 1 to 10.
$awk 'BEGIN { for(i=1;i -le10;i++) print " Square of value: ", i, "is",i*i; } '
$awk ' { Total_Salary += $3 } END { print Total_Salary } ' test_awk.dat
11) To find/check for any string in any specific column.
$ awk '{ if($1 == "101") print $0;} ' test_awk.dat
#unixbasics #reporting #unix #unixtutorial #reporting #unix #unixtutorial #awk #awkcommand #text #learning #coding #programming
Негізгі бет Unix Basics | awk | Frequently used commands | text processing , reporting
Пікірлер