eg, each row of the file is like :

1, 2, 3, 4,..., 1000

How can print out

1 2 3 4 ... 1000

?

有帮助吗?

解决方案

If you just want to delete the commas, you can use tr:

$ tr -d ',' <file
1 2 3 4 1000

If it is something more general, you can set FS and OFS (read about FS and OFS) in your begin block:

awk 'BEGIN{FS=","; OFS=""} ...' file

其他提示

You need to set OFS (the output field separator). Unfortunately, this has no effect unless you also modify the string, leading the rather cryptic:

awk '{$1=$1}1' FS=, OFS=

Although, if you are happy with some additional space being added, you can leave OFS at its default value (a single space), and do:

awk -F, '{$1=$1}1'

and if you don't mind omitting blank lines in the output, you can simplify further to:

awk -F, '$1=$1'

You could also remove the field separators:

awk -F, '{gsub(FS,"")} 1'

Set FS to the input field separators. Assigning to $1 will then reformat the field using the output field separator, which defaults to space:

awk -F',\s*' '{$1 = $1; print}'

See the GNU Awk Manual for an explanation of $1 = $1

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top