Skip to main content
Learn how to use the -F option in awk to customize the field separator when processing structured text. By default, awk splits records on whitespace, but -F lets you define any character or regular expression as the delimiter.

1. Default Field Splitting

By default, awk treats any whitespace as the field separator:
When your data uses a different delimiter—such as commas, colons, or pipes—you must override this behavior.

2. Changing the Field Separator with -F

To use a colon (:) as the separator:
Print the second field:
Combine fields 1 and 5:
You can also pipe data into awk:

3. Why Quote or Escape the Separator

Certain characters (for example, |, &, *, <, >) have special meanings to the shell.
Always quote or escape the field separator to prevent shell interpretation.Incorrect:
This fails because | is seen as a pipe.Correct:
The image shows a tip about using the awk command with a field separator, suggesting to enclose the character in double quotes for literal values. There's also a lightbulb icon and a checkmark.

4. Common Field Separators

The image shows a list of special characters separated by dotted lines, under the heading "awk -F - Field Separator."

5. Example: Processing Database Output

Here’s a Bash script (db.sh) that runs a PostgreSQL query inside Docker, trimming each field:
View the output:
Extract first names (field 2):
See the GNU Awk User’s Guide for more awk options.

Next Steps

Next, we’ll explore the -v option to declare variables within awk:

References

Watch Video