Skip to content
Latchkey

awk OFS and ORS: Output Field and Record Separators

awk OFS sets the string placed between fields by print, and ORS sets the string placed after each record.

OFS and ORS are the output mirror of FS. They let you re-delimit data, for example turning comma-separated input into tab-separated output.

What it does

When print joins fields with a comma (print $1, $2), it inserts OFS between them; OFS defaults to a single space. ORS is appended after each print, defaulting to a newline. Reassigning $0 or any field also re-joins the record using OFS.

Common usage

Terminal
# CSV in, TSV out
awk -F, 'BEGIN{OFS="\t"} {print $1, $2, $3}' data.csv
# force a rebuild so OFS is applied to the whole line
awk -F, 'BEGIN{OFS="\t"} {$1=$1; print}' data.csv
# join records with a comma instead of newlines
awk 'BEGIN{ORS=","} {print $1}' ids.txt

Variables

VariableWhat it does
OFSOutput field separator inserted by print between comma-listed args (default space)
ORSOutput record separator appended after each print (default newline)
$1=$1Idiom that forces awk to rebuild $0 using the new OFS
BEGIN{OFS="\t"}Set OFS once before any line is read

In CI

Changing OFS alone does not rewrite a line you only print as $0; you must reassign a field (the $1=$1 idiom) to trigger the rebuild. This catches people converting delimiters: print $0 keeps the original separators, print after $1=$1 uses OFS.

Common errors in CI

Setting OFS but seeing the old separator means you printed $0 without forcing a rebuild; add $1=$1 first or list the fields explicitly. An ORS of "," leaves no trailing newline, so the last value runs into the next shell prompt or token; append a newline yourself if a consumer needs one.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →