I'm running REHL7 at work with column -V at column from util-linux 2.23.2
I have csv files that contain some columns with long strings. I want to view the csv as a table, and limit the column width since I'm typically not interested in spot checking the long strings.
cat foo_bar.csv | column -s"," -t -c5
It appears that the column width is not being limited to 10 chars. I wonder if this is a bug, or I'm doing it wrong and can't see it ?
Test input, test.csv
co1,col2,col3,col4,col5
1,2,3,longLineOfTextThatIdoNotWantToInspectAndWouldLikeToLimit,5
Running the command I think is correct:
cat test.csv | column -s"," -t -c5
co1 col2 col3 col4 col5
1 2 3 longLineOfTextThatIdoNotWantToInspectAndWouldLikeToLimit 5
The −c or −−columns option does not do what you think it does. By default,
column looks at all lines to find the longest one. If column can fit 2 of
those lines in 80 width, then every 2 lines are fit on one:
$ cat file
1 this is a short line
2 this is a short line
3 this line needs to be 39 or less char
4 this line needs to be 39 or less char
$ column file
1 this is a short line 3 this line needs to be 39 or less char
2 this is a short line 4 this line needs to be 39 or less char
$ column -x file
1 this is a short line 2 this is a short line
3 this line needs to be 39 or less char 4 this line needs to be 39 or less char
If you put -c lower than 80, it’s going to make it less likely that you get
more than 1 column:
$ column -c70 file
1 this is a short line
2 this is a short line
3 this line needs to be 39 or less char
4 this line needs to be 39 or less char
So, simply said, column cannot do what you want it to do. Awk can do this:
BEGIN {
FS = ","
}
{
for (x = 1; x <= NF; x++) {
printf "%s%s", substr($x, 1, 5), x == NF ? "\n" : "\t"
}
}
Result:
co1 col2 col3 col4 col5
1 2 3 longL 5
I was looking for a solution to a similar problem (truncating columns in docker ps output). I did not want to use awk and instead solved it using sed.
In this example I limit the output of docker ps to 30 characters per column.
docker ps -a --format "table {{.ID}},{{.Names}},{{.Image}},{{.State}},{{.Networks}}" | \
sed "s/\([^,]\{30\}\)[^,]*/\1/g" | \
column -s "," -t
The pattern matches 30 non-delimter ([^,]) characters in a group followed by the rest of the non-delimeter characters (if the column is less than 30 characters then it doesn't match and is left alone). The replacement is just the group of 30 characters and the rest of the column is discarded.
Just for fun, you could also do a mid-column truncation in case there is useful information at both ends of the column.
docker ps -a --format "table {{.ID}},{{.Names}},{{.Image}},{{.State}},{{.Networks}}" | \
sed "s/\([^,]\{14\}\)[^,]*\([^,]\{14\}\)/\1..\2/g" | \
column -s "," -t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With