awk OFS field separator with examples
Awk OFS field separator variable, you can combine the output fields according to the set delimiter.
In the last article, we introduced the awk FS variable, which is a pair with awk OFS.
Awk FS separates character strings into fields according to delimiters, and awk OFS connects output fields according to delimiters.
Okay, let’s see how to use awk OFS variables in linux/unix to connect output fields according to delimiters.
awk OFS examples
Default OFS uses blank to output field separators
Without using OFS variables, awk uses blank spaces in the default output field. As in the following example, we do not use awk OFS variables.
➜ ~ echo "a#b|c#d#e:f" | awk 'BEGIN{FS="[:#|]";}{print $2, $5}'
b e
OFS uses tab to output field separators
In the following example, we will set OFS to tab as the output character separator in the awk BEGIN block.
➜ ~ echo "a#b|c#d#e:f" | awk 'BEGIN{FS="[:#|]";OFS="\t"}{print $2, $5}'
b e

OFS is set to new line
In some cases, we need to output the field by line (output a new line). In this case, we need to set OFS to “\n“.
➜ ~ echo "a#b|c#d#e:f" | awk 'BEGIN{FS="[:#|]";OFS="\n"}{print $2, $5}'
b
e

Awk OFS variables can help us to normalize the format during data analysis, so that we can facilitate subsequent processing.