xen*_*ide 98 command-line shell text-processing
如果我的命令输出很长(单行),但我知道我只想要输出的第一个 [x](假设是 8 个)字符,那么最简单的方法是什么?没有任何分隔符。
Ste*_*n D 133
一种方法是使用cut:
command | cut -c1-8
Run Code Online (Sandbox Code Playgroud)
这将为您提供每行输出的前 8 个字符。由于cut是 POSIX 的一部分,它很可能在大多数 Unices 上。
use*_*606 37
这些是仅获取前 8 个字符的其他一些方法。
command | head -c8
command | awk '{print substr($0,1,8);exit}'
command | sed 's/^\(........\).*/\1/;q'
Run Code Online (Sandbox Code Playgroud)
如果你有 bash
var=$(command)
echo ${var:0:8}
Run Code Online (Sandbox Code Playgroud)
小智 11
使用参数展开的另一种线性解决方案
echo ${word:0:x}
EG: word="Hello world"
echo ${word:0:3} or echo ${word::3}
o/p: Hel
EG.2: word="Hello world"
echo ${word:1:3}
o/p: ell
Run Code Online (Sandbox Code Playgroud)