Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要打印字符串的最后 20 个字符,但只打印整个单词。分隔符是一个空格“ ”。让我们考虑这个例子:
string="快速棕狐跳过懒狗" echo $string | 尾巴-c20
返回s over the lazy dog。我需要它来over the lazy dog代替。你知道如何做到这一点吗?谢谢!
s over the lazy dog
over the lazy dog
echo $string | perl -ne 'print "$1\n" if /\b(\S.{0,20})$/'
echo $string | rev | cut -d ' ' -f -20
这适用于 Bash > 3.2,无需使用任何外部程序:
[[ $string =~ \ (.{0,20})$ ]] result="$BASH_REMATCH[1]"
我以UdiM 的 grep版本为基础。
grep