Grabbing the first [x] characters for a string from a pipe

One way is to use cut:

 command | cut -c1-8

This will give you the first 8 characters of each line of output. Since cut is part of POSIX, it is likely to be on most Unices.


These are some other ways to get only first 8 characters.

command | head -c8

command | awk '{print substr($0,1,8);exit}' 

command | sed 's/^\(........\).*/\1/;q'

And if you have bash

var=$(command)
echo ${var:0:8}

Another one liner solution by using parameter expansion

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