问题
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