Cutting last few characters of file name

Dear Friends,
Here I have two queries.

  1. Want to make folder named as last three characters of a file name (length of file name can vary file to file)
    E.g File name is AAAACCCCBBBB.txt (12 Characters excluding file extension) then folder to be created is BBB

Irrespective of length of file name, script should use only last 3 characters (Excluding file extension) to make folder.

  1. What can i do to grep 50th line of each txt file in a directory?

:)Waiting for your reply

1:sed 's/^.*\(...\)\..*/\1/'

2:sed -n '1,50 p'

Dear friend, i m new to Unix... can u please suggest me some easy to understand commands... especially for 1st query

if you have Python and can use it, here's an alternative

import os
DIR="/home/path"
for files in os.listdir(DIR):
    if os.path.isfile(files) and files.startswith("AAAA"):
        folderToBeCreated = files.split(".")[:-1][-1][-3:]
        folderToBeCreated = os.path.join(DIR,folderToBeCreated )
        if not os.path.exists( folderToBeCreated ):
            os.mkdir(folderToBeCreated)
import os
DIR="/home/path"
for files in os.listdir(DIR):
    if os.path.isfile(files) and files.startswith("AAAA"):
        for n,lines in enumerate(open(files)):
            if n==49: 
                print lines.strip()  #print 50th line
                break

Dear frnds,
I have tried solution given by Fedora (sed 's/^.*\(...\)\..*/\1/'), but its not working (Nothing is happening).. please tell me if there is some alternate solution?

What did you try and is not working?

echo 'AAAACCCCBBBB.txt' | sed 's/^.*\(...\)\..*/\1/'

Thanx for your reply. Its working fine. I have a request. Can some one please please explain me this command briefly?
I got it till echo 'AAAACCCCBBBB.txt' | sed 's/^.\(...\)
but didnt get remaining part i.e \..
/\1/'

echo 'AAAACCCCBBBB.txt' | sed 's/^.*\(...\)\../\1/'
sed 's/^.
\(...\) --> you understood till this part right??

\..* --> this is string after the ".". In this case anything after dot ( . ) like .txt . " \. " is the escape sequence for the dot(.).

\1 --> this is the first variable which you have captured by the pattern
" \(...\) "