如何使用linux限制行中存在的字符串长度

Jan*_*ora 11 linux bash ubuntu

我有以下形式的数据:

num1    This is a string
num2    This is another string
Run Code Online (Sandbox Code Playgroud)

我想限制在第一个标签之后的所有字符串的长度.这样长度(字符串)<4.因此,我得到的输出是:

num1    This is a string
num2    This is another 
Run Code Online (Sandbox Code Playgroud)

我可以使用python来做到这一点.但我试图找到一个Linux等价物,以实现相同.

jra*_*rez 21

您可以使用以下命令限制字符串,在本例中,从索引0到索引17.

$ var="this is a another string"

$ echo ${var:0:17}

this is a another
Run Code Online (Sandbox Code Playgroud)


Gil*_*not 15

使用,按列:

$ awk '{print $1, $2, $3, $4}' file
Run Code Online (Sandbox Code Playgroud)

或者用 :

sed -r 's@^(\S+\s+\S+\s+\S+\s+\S+).*@\1@' file
Run Code Online (Sandbox Code Playgroud)

或使用的长度:

$ cut -c 1-23 file
Run Code Online (Sandbox Code Playgroud)