How to limit the length of a string present in a string using linux

I have data of the following type:

num1 This is a string num2 This is another string 

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

 num1 This is a string num2 This is another 

I can do it with python. But I'm trying to find the linux equivalent to achieve the same.

+7
linux bash ubuntu
source share
3 answers

Using awk in columns:

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

or sed :

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

or in length using cut :

 $ cut -c 1-23 file 
+12
source share

You can use the following to limit the row in this case from index 0 to index 17.

 var="this is a another string" echo ${var:0:17} this is a another 
+14
source share

If you want to truncate lines at word boundaries, you can use fold with the -s option:

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

The disadvantage of fold and sed must be called for each line ( sed q matches tail -n1 ).

0
source share

All Articles