copied to clipboard!
string awk

AWK FS Fundamentals: Essential and Applied Techniques

updated: 2026/05/05 created: 2026/05/03

Introduction

When you want to process text efficiently, awk is a powerful tool that shines.

It is especially powerful when dealing with data split by delimiters, such as log analysis and CSV processing. One of the most important concepts in this context is fs.

This is called the field separator, and it plays the role of determining how input data is divided.

Understanding the field separator mechanism in awk allows you to go beyond simple text processing and perform complex data formatting and extraction.

However, it is also a point where beginners tend to stumble. For example, the difference between default behavior and explicit specification, and flexible splitting using regular expressions, can easily lead to unintended results if your understanding is vague.

This article explains the field separator in awk from the basics to advanced usage as a single flow.

By reading through each heading in order, you will naturally learn the role of fs and how to use it in practice.

Reference: GNU awk

The Role and Mechanism of the Field Separator (FS) in AWK

Creating the File

cat << 'EOF' > input.txt apple,100,red banana,200,yellow grape,300,purple EOF

Command

awk -F',' '{print $1, $2, $3}' input.txt

Output

apple 100 red
banana 200 yellow
grape 300 purple

Command

awk -F',' '{print $2}' input.txt

Output

100
200
300

How It Works

ItemDescription
FS (Field Separator)The delimiter used to split input lines
Default FSSpace or tab
-F','Split using comma as delimiter
$1, $2, $3Access each split field
Processing flowRead one line → Split by FS → Store in field variables

Explanation

FS is the standard for splitting line data, and is an essential setting when handling formats like CSV.
By specifying it appropriately, you can flexibly extract data with awk.

The Difference Between the Default Delimiter and Space/Tab Behavior

Creating the File

cat << 'EOF' > input.txt a b c a b c a b c EOF

※ To insert a tab, press Ctrl+v followed by the Tab key.

Command

awk '{print $1 "|" $2 "|" $3}' input.txt

Output

a|b|c
a|b|c
a|b|c

Command

awk 'BEGIN{FS=" "}{print $1 "|" $2 "|" $3}' input.txt

Output

a|b|c
a|b|c
a|b|c

Command

awk 'BEGIN{FS="\t"}{print $1 "|" $2 "|" $3}' input.txt

Output

a b   c||
a|b|c
a  b|c|

How It Works

SettingDelimiter HandlingSpaceTabConsecutive Delimiters
Default (FS not set)Treats whitespace collectively as one delimiterMerged
FS=" "Special handling (equivalent to default)Merged
FS="\t"Tab only as delimiter×Treated individually

Explanation

The key point is that awk's FS=" " is special — it treats spaces, tabs, and consecutive whitespace collectively as a single delimiter.
On the other hand, when you explicitly specify something like FS="\t", only that character is strictly used as the delimiter.

How to Specify a Delimiter Using the Command-Line Option “-F”

Creating the File

cat << 'EOF' > input.txt name:age:city Alice:25:Tokyo Bob:30:Osaka Charlie:35:Nagoya EOF

Command

awk -F ':' '{print $1, $3}' input.txt

Output

Alice Tokyo
Bob Osaka
Charlie Nagoya

Command

awk 'BEGIN {FS=":"} {print $2}' input.txt

Output

age
25
30
35

How It Works

ItemDescription
-F optionSpecifies the field delimiter (FS)
FSThe delimiter variable used internally by awk
$1, $2...Each split field
Default FSWhitespace (space or tab)

Explanation

Using -F allows you to easily change the delimiter from the command line.
Since FS is an internal variable, it can also be set in the same way within the BEGIN block.

Benefits and Usage of Defining the Variable “FS” Inside the BEGIN Block

Creating the File

cat << 'EOF' > input.txt apple,100 banana,200 orange,300 EOF

Command

awk 'BEGIN { FS="," } { print $1, $2 }' input.txt

Output

apple 100
banana 200
orange 300

Command

awk -F "," '{ print $1, $2 }' input.txt

Output

apple 100
banana 200
orange 300

How It Works

ItemDescription
FSVariable that specifies the field delimiter
BEGIN blockExecuted once before input processing begins
Defining FS in BEGINAllows consistent delimiter settings across all records
-F optionA quick way to specify FS from the command line
$1, $2Access to each split field

Explanation

Defining FS inside BEGIN allows the configuration to be self-contained within the script, improving readability.
It also improves reusability since it does not depend on external options.

Splitting Complex Pattern Data by Specifying a Regular Expression as FS

Creating the File

cat << 'EOF' > input.txt id:1001|name=Alice,age=25 id:1002|name=Bob,age=30 id:1003|name=Charlie,age=22 EOF

Command

awk -F '[:|=,]' '{print $2, $4, $6}' input.txt

Output

1001 Alice 25
1002 Bob 30
1003 Charlie 22

How It Works

ElementDescription
-F '[:|=,]'Split on any of the specified symbols
$2The id value (e.g. 1001)
$4The name value (e.g. Alice)
$6The age value (e.g. 25)
awk behaviorSplits one line using the specified regular expression and treats the results as fields

Explanation

In awk, specifying a regular expression for -F allows you to handle multiple delimiters simultaneously.
This lets you simply split and extract even complexly formatted data.

Settings for Accurately Processing Comma-Delimited (CSV) Files

Creating the File

cat << 'EOF' > input.txt name,age,city Alice,30,Tokyo Bob,25,Osaka Charlie,35,Nagoya EOF

Command

awk -F',' '{print $1, $2, $3}' input.txt

Output

name age city
Alice 30 Tokyo
Bob 25 Osaka
Charlie 35 Nagoya

Command

awk -F',' '{print $1}' input.txt

Output

name
Alice
Bob
Charlie

Command

awk 'BEGIN{FS=","} {print $2}' input.txt

Output

age
30
25
35

How It Works

ElementDescription
-F','Sets the field delimiter (FS) to a comma
FS=","Sets the delimiter in the BEGIN block
$1,$2,$3Retrieves column 1, 2, and 3
$0The entire line
awkA command for processing text column by column

Explanation

awk can accurately split CSV by specifying the delimiter with FS.
For comma-delimited data, the basic approach is to use -F',' or FS=",".

How to Specify Multiple Characters as Delimiters All at Once

Creating the File

cat << 'EOF' > input.txt apple,orange;banana|grape dog;cat,bird|fish EOF

Command

awk -F '[,;|]' '{print $1, $2, $3, $4}' input.txt

Output

apple orange banana grape
dog cat bird fish

How It Works

ElementDescription
-FSpecifies the field delimiter (FS)
[,;|]Treats comma, semicolon, and pipe as delimiters
$1,$2...References each split field
awkSplits and processes each line

Explanation

Since awk's FS accepts regular expressions, you can specify multiple delimiters at once.
Using a character class [] is simple and practical.

Combining with the “OFS” Variable to Control Output Delimiters

Creating the File

cat << 'EOF' > input.txt apple orange banana cat dog mouse red blue green EOF

Command

awk 'BEGIN {FS=" "; OFS=","} {print $1, $2, $3}' input.txt

Output

apple,orange,banana
cat,dog,mouse
red,blue,green

Command

awk 'BEGIN {FS=" "; OFS=" | "} {print $1, $3}' input.txt

Output

apple | banana
cat | mouse
red | green

How It Works

ElementRoleDescription
FS (Field Separator)Input delimiterThe delimiter used to split input data (e.g. space)
OFS (Output Field Separator)Output delimiterThe delimiter inserted between fields when using print
$1, $2, $3Field referenceEach column of split data
printOutput processingJoins fields using OFS and outputs them

Explanation

By splitting input with FS and freely controlling the output format with OFS, you can effectively reshape data. This is commonly used for CSV-style conversion and log processing.

Dynamically Changing FS Mid-Processing to Parse Multi-Level Structured Text

Creating the File

cat << 'EOF' > input.txt A:1,2,3 B:4,5,6 C:7,8,9 EOF

Command

awk -F':' '{ key=$1 FS="," split($2, arr, ",") for(i=1;i<=length(arr);i++){ print key, arr[i] } FS=":" }' input.txt

Output

A 1
A 2
A 3
B 4
B 5
B 6
C 7
C 8
C 9

How It Works

StepContentFS StateDescription
Read line:First split key and value using ":"
Get key:Store A/B/C in $1
Change FS,Switch field delimiter to comma
Run split,Split $2 into an array
Loop output,Expand and output the multi-level structure

Explanation

In awk, by changing FS mid-processing while using split, you can flexibly decompose hierarchical data.
This lets you handle multi-level structures within a single line simply.

Technique for Decomposing and Processing Text Character by Character by Setting FS to an Empty String

Creating the File

cat << 'EOF' > input.txt hello EOF

Command

awk 'BEGIN{FS=""} {for(i=1;i<=NF;i++) print $i}' input.txt

Output

h
e
l
l
o

How It Works

ElementDescription
FS=""Sets the field delimiter to an empty string
NFNumber of split fields (= number of characters)
$iThe i-th single character
for loopIterates through one character at a time

Explanation

Setting FS to an empty string causes awk to split fields one character at a time.
This allows character-level processing to be written simply.

Key Practical Points for Mastering awk and fs

In awk, fs is not merely a delimiter specification — it is an important element that determines the precision and flexibility of the entire text processing operation.

It is important to first understand the default behavior, then gradually learn how to configure it using -F and BEGIN, and further how to leverage regular expressions.

Especially at the beginner stage, you need to be careful about the behavior of space delimiters and the pitfalls in CSV processing.

Even if things appear to work correctly at a glance, they may malfunction depending on the data.

The deeper your understanding of awk and fs becomes, the greater the freedom you gain in text processing.

The quickest path to improvement is to first solidly grasp the basics, then gradually challenge yourself with more advanced applications.

Leave a Reply

Your email address will not be published. Required fields are marked *

©︎ 2025-2026 running terminal commands