AWK provides a set of powerful Inbuilt variables which helps in writing complex requirement & provides simplicity in the code. Some of the very frequent used Inbuilt variables in AWK scripting are covered in this article.
NR: Current count of the number of input records.
NF: Provides Number of fields.
FS: Contains the "field separator".
$0: Hold the complete record
$n: Represent the nth filed of the current record.
FNR:No of records in current file name.
OFS:Stores the "output field separator".
FILENAME: The name of the current input-file.
AWK Syntax
BEGIN { Initialisation}
{ Execution }
END { END block }
Input File:F_Data_File.txt
1001|A1|10|Y
1002|A2|20|Y
1003|A3|30|N
1004|A4|10|Y
1005|A5|10|N
1006|A6|20|N
1007|A7|20|N
1008|A8|30|N
NR: Current count of the number of input records.
NF: Provides Number of fields.
FS: Contains the "field separator".
$0: Hold the complete record
$n: Represent the nth filed of the current record.
FNR:No of records in current file name.
OFS:Stores the "output field separator".
FILENAME: The name of the current input-file.
AWK Syntax
BEGIN { Initialisation}
{ Execution }
END { END block }
Input File:F_Data_File.txt
1001|A1|10|Y
1002|A2|20|Y
1003|A3|30|N
1004|A4|10|Y
1005|A5|10|N
1006|A6|20|N
1007|A7|20|N
1008|A8|30|N
F, FS & OFS
$ awk -F"|" '{print}' F_Data_File.txt
Will display complete file on terminal, F is used to instruct AWK that the file is pipe(|) separated.
1001|A1|10|Y
1002|A2|20|Y
1003|A3|30|N
1004|A4|10|Y
1005|A5|10|N
1006|A6|20|N
1007|A7|20|N
1008|A8|30|N
$ awk -F"|" '{print $0}' F_Data_File.txt
Will display complete file on terminal same as in the previous example.
$ awk 'BEGIN{FS="|"}{print $1}' F_Data_File.txt
Will print first field of the file, here we have not used -F switch, instead we have used FS inbuilt variable. By processing this variable AWK understand that the input file is pipe separated. We must initialise the FS in BEGIN section as it is the part which execute first when AWK command executed.
$ awk 'BEGIN{FS="|";OFS=","}{print $1,$4}' F_Data_File.txt
1001,Y
1002,Y
1003,N
1004,Y
1005,N
1006,N
1007,N
1008,N
Input File:F_Data_File.txt
1001|A1|10|Y
1002|A2|20|Y
1003|A3|30
1004|A4|10|Y
1005|A5|10
NF
$ awk -F"|" '{print NF}' F_Data_File.txt
Above command will return below output:
4
4
3
4
3
As 1st, 2nd & 4th row has 4 columns/filed hence returned 4 and 3rd & 5th rows are having 3 columns/field hence returned 3.
If you want to print last filed of the file.
$ awk -F"|" '{print $NF}' F_Data_File.txt
It will return the last field of all the rows
Y
Y
30
Y
10
This will be very use full when you want to check/fetch last field from the file where you don't know how many fields/columns your file have.
NR
$ awk -F"|" '{print NR":"$0}' F_Data_File.txt
Output:
1:1001|A1|10|Y
2:1002|A2|20|Y
3:1003|A3|30
4:1004|A4|10|Y
5:1005|A5|10
Explanation: NR provides the row number which AWK is reading from the file.
FNR
$ awk -F"|" 'BEGIN{print "NR", "FNR"}
{print NR, FNR":"$0}' F_Data_File.txt F_Data_File.txt
Output:
NR FNR
1 1:1001|A1|10|Y
2 2:1002|A2|20|Y
3 3:1003|A3|30
4 4:1004|A4|10|Y
5 5:1005|A5|10
6 1:1001|A1|10|Y
7 2:1002|A2|20|Y
8 3:1003|A3|30
9 4:1004|A4|10|Y
10 5:1005|A5|10
FILENAME
$ awk -F"|" '{print "V_File_Name->"FILENAME,"V_Total_Row->" NR, "V_Curr_Row->" FNR":"$0}' A1 A2
Output:
V_File_Name->A1 V_Total_Row->1 V_Curr_Row->1:1001|A1|10|Y
V_File_Name->A1 V_Total_Row->2 V_Curr_Row->2:1002|A2|20|Y
V_File_Name->A1 V_Total_Row->3 V_Curr_Row->3:1003|A3|30|N
V_File_Name->A1 V_Total_Row->4 V_Curr_Row->4:1004|A4|10|Y
V_File_Name->A1 V_Total_Row->5 V_Curr_Row->5:1005|A5|10|N
V_File_Name->A1 V_Total_Row->6 V_Curr_Row->6:1006|A6|20|N
V_File_Name->A1 V_Total_Row->7 V_Curr_Row->7:1007|A7|20|N
V_File_Name->A1 V_Total_Row->8 V_Curr_Row->8:1008|A8|30|N
V_File_Name->A2 V_Total_Row->9 V_Curr_Row->1:1001|A1|10|Y
V_File_Name->A2 V_Total_Row->10 V_Curr_Row->2:1002|A2|20|Y
V_File_Name->A2 V_Total_Row->11 V_Curr_Row->3:1003|A3|30
V_File_Name->A2 V_Total_Row->12 V_Curr_Row->4:1004|A4|10|Y
V_File_Name->A2 V_Total_Row->13 V_Curr_Row->5:1005|A5|10
Explanation: Output is self explanatory, FILENAME variable prints the name of the current processing file.
Will display the below output on terminal, output is comma(,) separated as we have passed the value of OFS inbuilt variable as comma. It will be very helpful where we have to change the delimiter of the output.
1001,Y
1002,Y
1003,N
1004,Y
1005,N
1006,N
1007,N
1008,N
NF, NR, FNR & FILENAME
1001|A1|10|Y
1002|A2|20|Y
1003|A3|30
1004|A4|10|Y
1005|A5|10
NF
$ awk -F"|" '{print NF}' F_Data_File.txt
Above command will return below output:
4
4
3
4
3
As 1st, 2nd & 4th row has 4 columns/filed hence returned 4 and 3rd & 5th rows are having 3 columns/field hence returned 3.
If you want to print last filed of the file.
$ awk -F"|" '{print $NF}' F_Data_File.txt
It will return the last field of all the rows
Y
Y
30
Y
10
This will be very use full when you want to check/fetch last field from the file where you don't know how many fields/columns your file have.
NR
$ awk -F"|" '{print NR":"$0}' F_Data_File.txt
Output:
1:1001|A1|10|Y
2:1002|A2|20|Y
3:1003|A3|30
4:1004|A4|10|Y
5:1005|A5|10
Explanation: NR provides the row number which AWK is reading from the file.
FNR
$ awk -F"|" 'BEGIN{print "NR", "FNR"}
{print NR, FNR":"$0}' F_Data_File.txt F_Data_File.txt
Output:
NR FNR
1 1:1001|A1|10|Y
2 2:1002|A2|20|Y
3 3:1003|A3|30
4 4:1004|A4|10|Y
5 5:1005|A5|10
6 1:1001|A1|10|Y
7 2:1002|A2|20|Y
8 3:1003|A3|30
9 4:1004|A4|10|Y
10 5:1005|A5|10
Explanation: Just have a look on NR and FNR, NR gives the number of rows AWK has processed and FNR gives the row number of current processing file. Here AWK processing same file twice.
FILENAME
$ awk -F"|" '{print "V_File_Name->"FILENAME,"V_Total_Row->" NR, "V_Curr_Row->" FNR":"$0}' A1 A2
Output:
V_File_Name->A1 V_Total_Row->1 V_Curr_Row->1:1001|A1|10|Y
V_File_Name->A1 V_Total_Row->2 V_Curr_Row->2:1002|A2|20|Y
V_File_Name->A1 V_Total_Row->3 V_Curr_Row->3:1003|A3|30|N
V_File_Name->A1 V_Total_Row->4 V_Curr_Row->4:1004|A4|10|Y
V_File_Name->A1 V_Total_Row->5 V_Curr_Row->5:1005|A5|10|N
V_File_Name->A1 V_Total_Row->6 V_Curr_Row->6:1006|A6|20|N
V_File_Name->A1 V_Total_Row->7 V_Curr_Row->7:1007|A7|20|N
V_File_Name->A1 V_Total_Row->8 V_Curr_Row->8:1008|A8|30|N
V_File_Name->A2 V_Total_Row->9 V_Curr_Row->1:1001|A1|10|Y
V_File_Name->A2 V_Total_Row->10 V_Curr_Row->2:1002|A2|20|Y
V_File_Name->A2 V_Total_Row->11 V_Curr_Row->3:1003|A3|30
V_File_Name->A2 V_Total_Row->12 V_Curr_Row->4:1004|A4|10|Y
V_File_Name->A2 V_Total_Row->13 V_Curr_Row->5:1005|A5|10
Explanation: Output is self explanatory, FILENAME variable prints the name of the current processing file.
No comments:
Post a Comment