To simplify the discussion, let N = 3
.
My current approach to extracting the last three characters of every line in a file or stream is to use sed
Why emphasize brevity when it's a tiny command either way? Generality is much more important:
$ cat file
123456789
abcdefghijklmn
To print 3 characters starting from the 4th character:
$ awk '{print substr($0,4,3)}' file
456
def
To print 3 characters starting from the 4th-last character:
$ awk '{print substr($0,length($0)-3,3)}' file
678
klm
To print 3 characters from [around] the middle of each line:
$ awk '{print substr($0,(length($0)-3)/2,3)}' file
345
efg