arunmoezhi
arunmoezhi

Reputation: 3200

multiple field separator in awk

I'm trying to process an input which has two field seperators ; and space. I'm able to parse the input with one separator using:

echo "10.23;7.15;6.23" | awk -v OFMF="%0.2f" 'BEGIN{FS=OFS=";"} {print $1,$2,$3}'
10.23;7.15;6.23

For an input with two seperators, I tried this and it doesn't parse both the seperators:

echo "10.23;7.15 6.23" | awk -v OFMF="%0.2f" 'BEGIN{FS=OFS=";" || " "} {print $1,$2,$3}'

Upvotes: 2

Views: 2464

Answers (1)

Ed Morton
Ed Morton

Reputation: 203189

You want to set FS to a character list:

awk -F'[; ]' 'script' file

and the other builtin variable you're trying to set is named OFMT, not OFMF:

$ echo "10.23;7.15 6.23" | awk -F'[; ]' -v OFMT="%0.2f" '{print $1,$2,$3}'
10.23 7.15 6.23

$ echo "10.23;7.15 6.23" | awk 'BEGIN{FS="[; ]"; OFS=";"; OFMT="%0.2f"} {print $1,$2,$3}'
10.23;7.15;6.23

Upvotes: 4

Related Questions