Grabbing the first [x] characters for a string from a pipe
If I have really long output from a command (single line) but I know I only want the first [x] (let's say 8) characters of the output, what's the easiest way to get that? There aren't any delimiters.
One way is to use
command | cut -c1-8
This will give you the first 8 characters of each line of output. Since
cutis part of POSIX, it is likely to be on most Unices.
Note that `cut -c` selects characters; `cut -b` or `head -c` selects bytes. This makes a difference in some locales (in practice, when using UTF-8).
You also don't have to specify the start index in this case. Saying `cut -c-8` will select from character 1 to 8.
Also `command | dd bs=8 count=1 2>/dev/null`. Not saying it's shorter or superior. Just another alternative.