With awk
:
To get the character at position 10, 1-indexed:
awk 'NR==2 {print substr($0, 10, 1)}'
NR==2
is checking if the record is second, if so the statements inside {}
would be executed
substr($0, 10, 1)
will extract 1 character starting from position 10 from field $0
(the whole record) i.e. only the 10-th character will be extracted. The format for substr()
is substr(field, offset, length)
.
Similarly, to get ±5 characters around 10-th:
awk 'NR==2 {print substr($0, (10-5), 11)}'
(10-5)
instead of 5
is just to give you the idea of the stuffs.
Example:
% cat file.txt
>header
cgatgcgctctgtgcgtgcgtgcg
% awk 'NR==2 {print substr($0, 10, 1)}' file.txt
c
% awk 'NR==2 {print substr($0, (10-5), 11)}' file.txt
gcgctctgtgc