开发者

Bash stdout character limit

开发者 https://www.devze.com 2023-02-20 10:50 出处:网络
开发者_StackOverflowHow can you limit the count of standard output characters that is redirected to file?Other ways (external)
开发者_StackOverflow

How can you limit the count of standard output characters that is redirected to file?


Other ways (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/'


You could use Command Substitution to wrap the output pre-redirection, then use the offset Parameter Expansion to limit the number of characters like so:

#!/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


You can't do so directly into a file, but you can pipe through sed or head, etc. to pass on only part of the output. Or as @SiegeX says, capture the output in the shell (but I would be wary of that if the output mis likely to be large).

0

精彩评论

暂无评论...
验证码 换一张
取 消