I have a file which contain 64895 characters
In [95]: !wc -c 07.org
64895 07.org
How could I get the character at position 60000?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityIf the file has newlines, then head
and tail
can be used to find a specific byte. For a file of ascii characters, chars are equivalent to bytes, but non-ascii unicode characters occupy multiple bytes. Also, the newline characters are counted. To get the byte at position 60000:
$ head -c 60000 file.txt | tail -c -1
To see how this works, the following loop looks at the first 9 bytes:
$ # First the data layout
$ echo $'123\n56\n89'
123
56
89
$ # Now get the first 9 bytes in turn
$ for i in `seq 9`; do c=`echo $'123\n56\n89' | head -c $i | tail -c -1` ; echo "$i => |$c|"; done
1 => |1|
2 => |2|
3 => |3|
4 => ||
5 => |5|
6 => |6|
7 => ||
8 => |8|
9 => |9|
Bytes 4 and 7 are newline characters.
There is the cut
command for that:
tr -d '\n' < 07.org | cut -c60000
The 1st part removes newlines; the 2nd part then prints the 60000th char (but does skip the newline char so if those need to be included in the count towards 60000 this wont work ;) )
wc -c
reports the number of bytes rather than the number of characters