如何限制使用linux在一行中的string的长度

我有以下forms的数据:

num1 This is a string num2 This is another string 

我想限制在第一个tab之后的所有string的长度。例如length(string)<4。 所以,我得到的输出是:

 num1 This is a string num2 This is another 

我可以使用python来做到这一点。 但是我正在试图find一个相当于linux的,以达到相同的目的。

使用awk ,按列:

 $ awk '{print $1, $2, $3, $4}' file 

或与sed :

 sed -r 's@^(\S+\s+\S+\s+\S+\s+\S+).*@\1@' file 

或者使用剪切长度:

 $ cut -c 1-23 file 

在这种情况下,可以使用以下命令来限制字符串,从索引0到索引17。

 var="this is a another string" echo ${var:0:17} this is a another 

如果你想截断字边界上的字符串,你可以使用-s选项的fold

 awk -F"\t" '{ printf "%s\t", $1; system(sprintf("fold -sw 17 <<< \"%s\" | sed q", $2)) }' 

缺点是需要为每行调用foldsedsed qtail -n1相同)。