Linux 从标准输出获取输出的最后 4 个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9219964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the last 4 characters of output from standard out
提问by bing281
I have a script that is running and uses
我有一个正在运行并使用的脚本
lspci -s 0a.00.1
This returns
这返回
0a.00.1 usb controller some text device 4dc9
I want to get those last 4 characters inline such that
我想内联最后 4 个字符,以便
lspci -s 0a.00.1 | some command to give me the last 4 characters.
采纳答案by Diego
How about tail
, with the -c
switch. For example, to get the last 4 characters of "hello":
怎么样tail
,用-c
开关。例如,要获取“hello”的最后 4 个字符:
echo "hello" | tail -c 5
ello
Note that I used 5 (4+1) because a newline character is added by echo
. As suggested by Brad Koch below, use echo -n
to prevent the newline character from being added.
请注意,我使用了 5 (4+1),因为echo
. 正如下面 Brad Koch 所建议的,用于echo -n
防止添加换行符。
回答by choroba
I usually use
我通常使用
echo 0a.00.1 usb controller some text device 4dc9 | rev | cut -b1-4 | rev
4dc9
回答by Birei
Using sed
:
使用sed
:
lspci -s 0a.00.1 | sed 's/^.*\(.\{4\}\)$//'
Output:
输出:
4dc9
回答by Johnsyweb
Do you really want the last four characters? It looks like you want the last "word" on the line:
你真的想要最后四个字符吗?看起来您想要该行的最后一个“单词”:
awk '{ print $NF }'
This will work if the ID is 3 characters, or 5, as well.
如果 ID 为 3 个字符或 5 个字符,这将起作用。
回答by cwallenpoole
One more way to approach this is to use <<<
notation:
解决这个问题的<<<
另一种方法是使用符号:
tail -c 5 <<< '0a.00.1 usb controller some text device 4dc9'
回答by Mingjiang Shi
Try this, say if the string is stored in the variable foo.
试试这个,假设字符串是否存储在变量 foo 中。
foo=`lspci -s 0a.00.1` # the foo value should be "0a.00.1 usb controller some text device 4dc9"
echo ${foo:(-4)} # which should output 4dc9
回答by Ralph Kirchner
If the real request is to copy the last space-separated string regardless of its length, then the best solution seems to be using ... | awk '{print $NF}'
as given by @Johnsyweb. But if this is indeed about copying a fixed number of characters from the end of a string, then there is a bash-specific solution without the need to invoke any further subprocess by piping:
如果真正的请求是复制最后一个空格分隔的字符串而不管其长度,那么最好的解决方案似乎是使用... | awk '{print $NF}'
@Johnsyweb 给出的方法。但是,如果这确实是从字符串的末尾复制固定数量的字符,那么有一个特定于 bash 的解决方案,而无需通过管道调用任何进一步的子进程:
$ test="1234567890"; echo "${test: -4}"
7890
$
Please note that the space between colon and minus character is essential, as without it the full string will be delivered:
请注意,冒号和减号之间的空格是必不可少的,因为没有它,将提供完整的字符串:
$ test="1234567890"; echo "${test:-4}"
1234567890
$
回答by kenorb
Try using grep
:
尝试使用grep
:
lspci -s 0a.00.1 | grep -o ....$
This will print last 4 characters of every line.
这将打印每行的最后 4 个字符。
However if you'd like to have last 4 characters of the whole output, use tail -c4
instead.
但是,如果您想要整个输出的最后 4 个字符,请tail -c4
改用。
回答by Marty McGowan
instead of using named variables, develop the practice of using the positional parameters, like this:
不要使用命名变量,而是开发使用位置参数的做法,如下所示:
set -- $( lspci -s 0a.00.1 ); # then the bash string usage:
echo ${1:(-4)} # has the advantage of allowing N PP's to be set, eg:
set -- $(ls *.txt)
echo # prints the 4th txt file.