Tutorial by Examples

Used by awk to split each record into multiple fields: echo "a-b-c d-e-f" | awk 'BEGIN {FS="-"} {print $2}' will result in: b e The variable FS can also be set using the option -F: echo "a-b-c d-e-f" | awk -F '-' '{print $2}' By default, the fields are se...
Used by awk to split the input into multiple records. For example: echo "a b c|d e f" | awk 'BEGIN {RS="|"} {print $0}' produces: a b c d e f By default, the record separator is the newline character. Similarly: echo "a b c|d e f" | awk 'BEGIN {RS="|&quo...
Used by awk to separate fields output by the print statement. For example: echo "a b c d e f" | awk 'BEGIN {OFS="-"} {print $2, $3}' produces: b-c e-f The default value is , a string consisting of a single space.
Used by awk to separate records and is output at the end of every print statement. For example: echo "a b c d e f" | awk 'BEGIN {ORS="|"} {print $2, $3}' produces: b c|e f The default value is \n (newline character).
Command line arguments passed to awk are stored in the internal array ARGV of ARGC elements. The first element of the array is the program name. For example: awk 'BEGIN { for (i = 0; i < ARGC; ++i) { printf "ARGV[%d]=\"%s\"\n", i, ARGV[i] } }' arg1 arg2 arg3 ...
The variable FS is used to set the input field separator. In awk, space and tab act as default field separators. The corresponding field value can be accessed through $1, $2, $3... and so on. awk -F'=' '{print $1}' file -F - command-line option for setting input field separator. awk 'BEGIN ...
This variable is used to set the output field separator which is a space by default. awk -F'=' 'BEGIN { OFS=":" } { print $1 }' file Example: $ cat file.csv col1,col2,col3,col4 col1,col2,col3 col1,col2 col1 col1,col2,col3,col4,col5 $ awk -F',' 'BEGIN { OFS="|" } { $...
This variable is used to set input record separator, by default a newline. awk 'BEGIN{RS=","} {print $0}' file
This variable is used to set output record separator, by default a newline. awk 'BEGIN{ORS=","} {print $0}' file
This variable will give you a total number of fields in the current input record. awk -F',' '{print NF}' file.csv Example: $ cat file.csv col1,col2,col3,col4 col1,col2,col3 col1,col2 col1 col1,col2,col3,col4,col5 $ awk -F',' '{print NF}' file.csv 4 3 2 1 5
Will provide the total number of records processed in the current awk instance. cat > file1 suicidesquad harley quinn joker deadshot cat > file2 avengers ironman captainamerica hulk awk '{print NR}' file1 file2 1 2 3 4 5 6 7 8 A total on 8 records were processed in th...
Provides the total number of records processed by the awk instance relative to the files awk is processing cat > file1 suicidesquad harley quinn joker deadshot cat > file2 avengers ironman captainamerica hulk awk '{print FNR}' file1 file2 1 2 3 4 1 2 3 4 Each file had...
Provides the number of columns or fields in each record (record corresponds to each line). Each line is demarcated by RS which defaults to newline. cat > file1 Harley Quinn Loves Joker Batman Loves Wonder Woman Superman is not dead Why is everything I type four fielded!? awk '{print NF}' ...
FNR contains the number of the input file row being processed. In this example you will see awk starting on 1 again when starting to process the second file. Example with one file $ cat file1 AAAA BBBB CCCC $ awk '{ print FNR }' file1 1 2 3 Example with two files $ cat file1 AAAA BBBB...

Page 1 of 1