Bash Character Values ​​stdout

How can you limit the number of standard output characters redirected to a file?

+4
source share
3 answers

You can use Replace Commands to wrap the preliminary redirection of the output, and then use the Offset Parameter Extension to limit the number of characters as follows:

#!/bin/bash limit=20 out=$(echo "this line has more than twenty characters in it") echo ${out::limit} > /path/to/file 

Proof of concept

 $ limit=20 $ out=$(echo "this line has more than twenty characters in it"). $ echo ${out::limit} this line has more t 
+1
source

Other methods (external)

 echo $out| head -c 20 echo $out | awk '{print substr($0,1,20) }' echo $out | ruby -e 'print $_[0,19]' echo $out | sed -r 's/(^.{20})(.*)/\1/' 
+4
source

You cannot do this directly in the file, but you can pass through sed or head , etc., to transfer only part of the output. Or, as @SiegeX says, grab the output in the shell (but I would be wary of this if the output value could be large).

0
source

Source: https://habr.com/ru/post/1345791/


All Articles