Jan Rostenkowski
Jan Rostenkowski

Reputation: 325

awk output into specified number of columns

EDIT: I can get all my desired values from the command:

awk '/Value/{print $4}' *.log > ofile.csv 

This makes a .csv file with a single column with hundreds of values. I would like to separate these values into a specified number of columns, i.e. instead of having values 1-1000 in a single column, I could specify that I want 100 columns and then my .csv file would have the first column be 1-10, 2nd column be 11-20... 100th column be 991-1000.

Previously, I was using the pr command to do this, but it doesn't work when the number of columns I want is too high (>36 in my experience).

awk '/Value/{print $4}' *.log | pr -112s',' > ofile.csv

the pr command gives the following message: pr: page width too narrow

Is there an alternative to this command that I can use, that won't restrict the amount of comma delimiters in a row of data?

Upvotes: 2

Views: 293

Answers (1)

duthils
duthils

Reputation: 1391

If your values are always the same length, you can use column:

$ seq 100 200 | column --columns 400 | column --table --output-separator "," 
100,103,106,109,112,115,118,121,124,127,130,133,136,139,142,145,148,151,154,157,160,163,166,169,172,175,178,181,184,187,190,193,196,199
101,104,107,110,113,116,119,122,125,128,131,134,137,140,143,146,149,152,155,158,161,164,167,170,173,176,179,182,185,188,191,194,197,200
102,105,108,111,114,117,120,123,126,129,132,135,138,141,144,147,150,153,156,159,162,165,168,171,174,177,180,183,186,189,192,195,198,

The --columns control the number of columns, but in my example, 400 is the number of characters, not the number of columns.

If your values are not the same character length, you will find spaces inserted where the values have a different width.

Upvotes: 2

Related Questions