That's a great question! Besides NR, awk has several other built-in variables that make text processing much easier. Here are the most useful ones:
1. NF (Number of Fields)
NF represents the number of fields (columns) in the current line.
- Use case: Finding lines that don't have the expected number of columns.
- Example:
awk '{print "Line " NR " has " NF " columns"}' awk_test.txt
2. FS (Field Separator)
By default, awk splits lines by spaces or tabs. FS lets you change that.
- Use case: Processing CSV files (where discovery is separated by commas).
- Example:
awk 'BEGIN {FS=","} {print $1}' data.csv - Note: You can also use the
-Fflag on the command line:awk -F',' '{print $1}' data.csv
3. OFS (Output Field Separator)
This defines how fields are separated when you print them using a comma.
- Use case: Converting a space-separated file to a dash-separated one.
- Example:
awk 'BEGIN {OFS="-"} {print $1, $2}' awk_test.txt
4. FILENAME
This variable stores the name of the file currently being processed.
- Use case: When processing multiple files at once, it helps you identify where the data came from.
- Example:
awk '{print FILENAME, $0}' *.txt
5. RS (Record Separator)
By default, a "record" is a single line (separated by a newline \n). RS allows you to define a different record separator.
- Use case: Processing data where records are separated by blank lines instead of single newlines.
Summary Table
| Variable | Meaning | Common Use |
|---|---|---|
NR |
Number of Records | Current line number |
NF |
Number of Fields | Number of columns in current line |
FS |
Field Separator | Input delimiter (default: space) |
OFS |
Output Field Separator | Output delimiter (default: space) |
FILENAME |
Filename | The name of the current input file |
You can try using NF on your awk_test.txt file now to see how many columns each line has