Show character at position in a file

If you want the 5th byte, counting from 1:

dd ibs=1 skip=4 count=1

or

tail -c +5 | head -c 1

Note that tail counts from 1, so given a file containing abcdefg, this prints e.

dd and tail -c are in POSIX. head -c is common, but isn't in POSIX; it's in GNU coreutils, BusyBox, FreeBSD and NetBSd and but not in OpenBSD or Solaris.


With sed:

$ echo 12345 | sed 's/.\{4\}\(.\).*/\1/;q'
5
$ echo 1234ắ | sed 's/.\{4\}\(.\).*/\1/;q'
ắ

Note that sed will fail to produce output if you input contain invalid multi-byte characters in current locale. You can use LC_ALL=C if you work with single byte characters only.

With ASCII file, you can also use dd:

$ echo 12345 | dd bs=1 skip=4 count=1 2>/dev/null
5

Or using (gnu)grep:

grep -zoP '.{4}\K.'   file

(-z was used to deal with \n before the 5th char)