![](/img/trans.png)
[英]How to multiple a number by 2 (double)present in a particular line number of a file, in Linux?
[英]how to restrict length of string present in a line using linux
我有以下形式的數據:
num1 This is a string
num2 This is another string
我想限制在第一個標簽之后的所有字符串的長度。這樣長度(字符串)<4。 因此,我得到的輸出是:
num1 This is a string
num2 This is another
我可以使用python來做到這一點。 但我試圖找到一個Linux等價物,以實現相同。
在bash中,您可以使用以下命令限制字符串,在本例中,從索引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))
}'
缺點是需要為每一行調用fold
和sed
( sed q
與tail -n1
相同)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.