awk OFS,获得意想不到的结果

时间:2014-09-15 23:10:16

标签: csv awk

我无法理解为什么我会在这里得到意想不到的结果,如果有人可以带来的话 对此有所了解: -

这是uk-500.csv输入文件的前5条记录:

"first_name","last_name","company_name","address","city","county","postal","phone1","phone2","email","web"
"Aleshia","Tomkiewicz","Alan D Rosenburg Cpa Pc","14 Taylor St","St. Stephens Ward","Kent","CT2 7PP","01835-703597","01944-369967","atomkiewicz@hotmail.com","http://www.alandrosenburgcpapc.co.uk"
"Evan","Zigomalas","Cap Gemini America","5 Binney St","Abbey Ward","Buckinghamshire","HP11 2AX","01937-864715","01714-737668","evan.zigomalas@gmail.com","http://www.capgeminiamerica.co.uk"
"France","Andrade","Elliott, John W Esq","8 Moor Place","East Southbourne and Tuckton W","Bournemouth","BH6 3BE","01347-368222","01935-821636","france.andrade@hotmail.com","http://www.elliottjohnwesq.co.uk"
"Ulysses","Mcwalters","Mcmahan, Ben L","505 Exeter Rd","Hawerby cum Beesby","Lincolnshire","DN36 5RP","01912-771311","01302-601380","ulysses@hotmail.com","http://www.mcmahanbenl.co.uk"

当我运行此命令时:

awk 'BEGIN { FS="\",?\"?"; OFS="=" } NR < 5 { print $3 }' uk-500.csv

我明白了:

last_name
Tomkiewicz
Zigomalas
Andrade

如果我使用:

awk 'BEGIN { FS="\",?\"?"; OFS="=" } NR < 5 { printf $3" " }' uk-500.csv

我明白了:

last_name Tomkiewicz Zigomalas Andrade

为什么在这两种情况下awk都会忽略OFS值,不应该得到:

=last_name=Tomkiewicz=Zigomalas=Andrade=

ADDITION

虽然我们处于一个主题,但值得一提的是,如果发生变化 FSOFS,可以预期普通printprint $0输出全部 每个记录重新计算值的字段,但这不会发生,因为 没有字段被更改,所以这个:

awk 'BEGIN { FS="\",?\"?"; OFS="=" } NR < 5 { print }' uk-500.csv

会产生这个:

"first_name","last_name","company_name","address","city","county","postal","phone1","phone2","email","web"
"Aleshia","Tomkiewicz","Alan D Rosenburg Cpa Pc","14 Taylor St","St. Stephens Ward","Kent","CT2 7PP","01835-703597","01944-369967","atomkiewicz@hotmail.com","http://www.alandrosenburgcpapc.co.uk"
"Evan","Zigomalas","Cap Gemini America","5 Binney St","Abbey Ward","Buckinghamshire","HP11 2AX","01937-864715","01714-737668","evan.zigomalas@gmail.com","http://www.capgeminiamerica.co.uk"
"France","Andrade","Elliott, John W Esq","8 Moor Place","East Southbourne and Tuckton W","Bournemouth","BH6 3BE","01347-368222","01935-821636","france.andrade@hotmail.com","http://www.elliottjohnwesq.co.uk"

这样做的正确方法是:

awk 'BEGIN { FS="\",?\"?"; OFS="=" } NR < 5 { $1=$1; print }' uk-500.csv

现在结果就像我们预期的那样:

=first_name=last_name=company_name=address=city=county=postal=phone1=phone2=email=web=
=Aleshia=Tomkiewicz=Alan D Rosenburg Cpa Pc=14 Taylor St=St. Stephens Ward=Kent=CT2 7PP=01835-703597=01944-369967=atomkiewicz@hotmail.com=http://www.alandrosenburgcpapc.co.uk=
=Evan=Zigomalas=Cap Gemini America=5 Binney St=Abbey Ward=Buckinghamshire=HP11 2AX=01937-864715=01714-737668=evan.zigomalas@gmail.com=http://www.capgeminiamerica.co.uk=
=France=Andrade=Elliott, John W Esq=8 Moor Place=East Southbourne and Tuckton W=Bournemouth=BH6 3BE=01347-368222=01935-821636=france.andrade@hotmail.com=http://www.elliottjohnwesq.co.uk=

1 个答案:

答案 0 :(得分:1)

OFS代表输出字段分隔符。默认设置为单个空格。使用printf时,永远不会使用OFS

您可能正在寻找的是ORS,它是输出记录分隔符,默认情况下设置为换行符。

设置ORS将为您提供以下输出。

$ awk 'BEGIN { FS="\",?\"?"; ORS="=" } NR < 5 { print $3 }' uk-500.csv
last_name=Tomkiewicz=Zigomalas=Andrade=

如果换行很重要,您可以使用END阻止。