Get the last 4 characters of output from standard out

bing281 picture bing281 · Feb 9, 2012 · Viewed 64.3k times · Source

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

lspci -s 0a.00.1 | some command to give me the last 4 characters. 

Answer

Diego picture Diego · Feb 9, 2012

How about tail, with the -c switch. For example, to get the last 4 characters of "hello":

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.