15

I have data of the following form:

num1    This is a string
num2    This is another string

I want to limit length of all strings which are after the first tab..such that length(string)<4. Therefore, the output which I get is:

num1    This is a string
num2    This is another 

I can do this using python. But I am trying to find a linux equivalent in order to achieve the same.

4

3 回答 3

27

在 bash 中,您可以使用以下内容来限制字符串,在这种情况下,从索引 0 到索引 17。

$ var="this is a another string"

$ echo ${var:0:17}

this is a another
于 2013-11-08T22:23:14.187 回答
18

使用,按列:

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

或使用

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

或使用长度:

$ cut -c 1-23 file
于 2013-11-08T22:28:56.877 回答
0

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

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

缺点是fold并且sed需要为每一行调用(sed q与 相同tail -n1)。

于 2013-11-09T00:24:37.753 回答