列,宽度参数不起作用

时间:2017-02-06 18:35:52

标签: linux shell unix awk multiple-columns

我正在使用column -V column from util-linux 2.23.2cat foo_bar.csv | column -s"," -t -c5 一起运行REHL7

我的csv文件包含一些带有长字符串的列。 我想将csv视为一个表,并限制列宽,因为我是 通常对检查长串不感兴趣。

test.csv

看来列宽不限于10个字符。 我想知道这是不是一个错误,或者我做错了什么并且看不到它?

测试输入,co1,col2,col3,col4,col5 1,2,3,longLineOfTextThatIdoNotWantToInspectAndWouldLikeToLimit,5

cat test.csv | column -s"," -t -c5 

co1  col2  col3  col4 col5
1    2     3     longLineOfTextThatIdoNotWantToInspectAndWouldLikeToLimit  5

运行我认为正确的命令:

CREATE TABLE IF NOT EXISTS availables (   
   IdMat     TINYINT,  
   size      INT, 
PRIMARY KEY(IdMat ) )

1 个答案:

答案 0 :(得分:1)

<div id='parent'> <table id='child1'> <colgroup> <col width='400px'></col> </colgroup> <tbody> <tr> <td>Child1withareallyreallylongtitle</td> </tr> </tbody> </table> <div id='child2'> <p>Child 2</p> </div> </div>−c选项不会按您的意图执行。默认情况下, −−columns查看所有行以找到最长的行。如果column适合2 那些80宽度的线,然后每2行适合一个:

column

如果你将$ 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 低于80,那么它就会降低你获得的可能性 超过1列:

-c

所以,简单地说,$ 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 不能做你想做的事。 Awk可以这样做:

column

结果:

BEGIN {
  FS = ","
}
{
  for (x = 1; x <= NF; x++) {
    printf "%s%s", substr($x, 1, 5), x == NF ? "\n" : "\t"
  }
}