donny
donny

Reputation: 99

transpose a column in unix

I have a Unix file which has data like this.

1379545632,   
1051908588,    
229102020,    
1202084378,    
1102083491,    
1882950083,    
152212030,    
1764071734,    
1371766009,

(FYI, there is no empty line between two numbers as you see above. Its just because of the editor here. Its just a column with all numbers one below other)

I want to transpose it and print as a single line.

Like this:

 1379545632,1051908588,229102020,1202084378,1102083491,1882950083,152212030,1764071734,1371766009

Also remove the last comma.

Can someone help? I need a shell/awk solution.

Upvotes: 2

Views: 3931

Answers (3)

Ashish Singh
Ashish Singh

Reputation: 11

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

ORS : Output Record separator. Each Record will be separated with this delimiter.

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203597

With GNU awk for multi-char RS:

$ printf 'x,\ny,\nz,\n' | awk -v RS='^$' '{gsub(/\n|(,\n$)/,"")} 1'
x,y,z

Upvotes: 1

John Zwinck
John Zwinck

Reputation: 249153

tr '\n' ' ' < file.txt

To remove the last comma you can try sed 's/,$//'.

Upvotes: 3

Related Questions