Transpose column to row without separator in the end [duplicate]

寵の児 提交于 2020-01-17 14:02:21

问题


I transposed column to row:

awk 'BEGIN { ORS = "," } { print }' file

File:

45
78
45

The result is:

45,78,45,

How to delete the comma in the end of row? I tried:

awk 'BEGIN { ORS = "," } { print substr($1, 1, length($1)-1)}' file

But it doesn't work for separator.


回答1:


For such a simple case the paste command is straightforward:

$ paste -s -d ',' file
45,78,45



回答2:


another awk, assumes number list has no empty line breaks

$ awk -v RS= -v OFS=, '{$1=$1}1' file



回答3:


You should use a different approach, like:

awk 'NR>1{printf "%s,",p} {p=$0} END{print p}' file

It prints <previous line>, whenever it reads a new line, and at the end of the input, it prints the last line.




回答4:


$ awk '{printf "%s%s", ors, $0; ors=","} END{print ""}' file
45,78,45


来源:https://stackoverflow.com/questions/58382633/transpose-column-to-row-without-separator-in-the-end

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!