Get the last 4 characters of output from standard out

后端 未结 9 1980
借酒劲吻你
借酒劲吻你 2020-12-24 13:00

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
         


        
9条回答
  •  时光取名叫无心
    2020-12-24 13:57

    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:

    $ 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
    $
    

提交回复
热议问题