Get the last 4 characters of output from standard out

后端 未结 9 1982
借酒劲吻你
借酒劲吻你 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:32

    Try using grep:

    lspci -s 0a.00.1 | grep -o ....$
    

    This will print last 4 characters of every line.

    However if you'd like to have last 4 characters of the whole output, use tail -c4 instead.

    0 讨论(0)
  • 2020-12-24 13:35

    One more way to approach this is to use <<< notation:

    tail -c 5 <<< '0a.00.1 usb controller some text device 4dc9'
    
    0 讨论(0)
  • 2020-12-24 13:39

    Using sed:

    lspci -s 0a.00.1 | sed 's/^.*\(.\{4\}\)$/\1/'
    

    Output:

    4dc9
    
    0 讨论(0)
  • 2020-12-24 13:40

    I usually use

    echo 0a.00.1 usb controller some text device 4dc9 | rev | cut -b1-4 | rev
    4dc9
    
    0 讨论(0)
  • 2020-12-24 13:42

    Try this, say if the string is stored in the variable 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
    
    0 讨论(0)
  • 2020-12-24 13:52

    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.

    0 讨论(0)
提交回复
热议问题