Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Which is the first field in awk print action?


Asked by Forrest O’brien on Nov 29, 2021 FAQ



So, $1 represents the first field, which we’ll use with the print action to print the first field. awk prints the first field and discards the rest of the line.
Similarly,
The print statement outputs data from the file. When awk reads a record, it divides the record into fields based on the FS (input field separator) variable. This variable is predefined in awk to be one or more spaces or tabs. The variables $1, $2, $3 hold the values of the first, second, and third fields.
In addition, By default, awk considers a field to be a string of characters surrounded by whitespace, the start of a line, or the end of a line. Fields are identified by a dollar sign ( $) and a number. So, $1 represents the first field, which we’ll use with the print action to print the first field.
Also,
AWK provides a built-in length function that returns the length of the string. $0 variable stores the entire line and in the absence of a body block, default action is taken, i.e., the print action. Hence, if a line has more than 18 characters, then the comparison results true and the line gets printed. Previous Page Print Page
Next,
This is probably one of the most common use cases for AWK: extracting some columns of the data file. Here, I explicitly set both the input and output field separators to the coma. When AWK split a record into fields, it stores the content of the first field into $1, the content of the second field into $2 and so on.