Cutting last few characters of file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting last few characters of file name
# 1  
Old 09-23-2008
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.


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

SmilieWaiting for your reply
# 2  
Old 09-23-2008
1:sed 's/^.*\(...\)\..*/\1/'

2:sed -n '1,50 p'
# 3  
Old 09-23-2008
Dear friend, i m new to Unix... can u please suggest me some easy to understand commands... especially for 1st query
# 4  
Old 09-23-2008
Quote:
Originally Posted by anushree.a
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.
if you have Python and can use it, here's an alternative
Code:
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)

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

SmilieWaiting for your reply
Code:
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

# 5  
Old 09-30-2008
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?
# 6  
Old 09-30-2008
Quote:
Originally Posted by anushree.a
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?
Code:
echo 'AAAACCCCBBBB.txt' | sed 's/^.*\(...\)\..*/\1/'

# 7  
Old 09-30-2008
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/'

Last edited by anushree.a; 09-30-2008 at 02:18 AM.. Reason: To Simplify the prob
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cutting string values from a file

My file looks like this.... User:SYSTEM,O/S User:oracle,Process:3408086,Machine:hostname ,Program:sqlplus@hostname (TNS V1-V,Logon Time:25-JUL-20 14 13:36 I want to get the date and time which is displayed after the 'Logon time'. (5 Replies)
Discussion started by: Nagesh_1985
5 Replies

2. Shell Programming and Scripting

Cutting file name from end

Dear Friends, I want to take last 10 characters of a file name in a variable. E.g. file name is 123432311234567890.txt then we want "1234567890" to be taken in an variable. Request you to guide me to do the same Thanks in anticipation Anushree (7 Replies)
Discussion started by: anushree.a
7 Replies

3. Shell Programming and Scripting

Cutting characters in a variable

I have a variable var which contains "ABCDEFDH" Now I have to remove the first 4 characters that is "ABCD" so my variable should contain only "DEFH" plzz tell me how to do that . i am using bash shell (1 Reply)
Discussion started by: cynosure2009
1 Replies

4. Shell Programming and Scripting

Trouble cutting characters into a string.

I just have a couple of quick questions. I am having trouble with this cut. I am basically trying to cut the string so that i can insert the users guess at the appropriate point in the string. $letters is the character count of the $word. What it seems to do is cut the character into the... (0 Replies)
Discussion started by: Makaer
0 Replies

5. Shell Programming and Scripting

Cutting specific lines from a file

Hi, I have a file named Mani.txt. The contents are like this cat Mani.txt -------------------------------------------------------- Hi there how r u My Name is Mani Bye ------------------------------------------------------------ I want to cut the first and last lines from the file... (15 Replies)
Discussion started by: pathanjalireddy
15 Replies

6. Shell Programming and Scripting

cutting a section of a big file

Hi, I have a text file 10giga size. Opening the file with vi takes forever ... Im intersting only with the 100 first records. Is there way to copy those 100 lines to new file (with no need to open the file)? Thanks (6 Replies)
Discussion started by: yoavbe
6 Replies

7. AIX

CUT command - cutting characters from end of string

Hello, I need to delete the final few characters from a parameter leaving just the first few. However, the characters which need to remain will not always be a string of the same length. For instance, the parameter will be passed as BN_HSBC_NTRS/hub_mth_ifce.sf. I only need the bit before the... (2 Replies)
Discussion started by: JWilliams
2 Replies

8. Shell Programming and Scripting

Cutting a tab delimiter file

I have a 30 column tab delimited record file. I need to extract the first 10column. The following command to cut was not working cut -f 1-10 -d "\t" filename. Could any one keep on this . Thanks in Advance (4 Replies)
Discussion started by: vinod.thayil
4 Replies

9. Shell Programming and Scripting

Cutting specific name from configuration file

Hi falks, I have the following configuration file structure: file1:N file2:Y file3:Y file4:N ...... I need to cut from the configuration file only the file with the sign "Y" in the end and copy it to some directory. What is the best way to do it? Thanks in advance, Nir (8 Replies)
Discussion started by: nir_s
8 Replies

10. UNIX for Dummies Questions & Answers

Cutting n consecutive lines from a file...

Hi, I have this problem of separating 10 consecutive lines from a file, say starting from 21 to 30... I have used a filter like this.. head -n 30 myfile | tail -n 10 Is there a simpler way than this? (2 Replies)
Discussion started by: Vishnu
2 Replies
Login or Register to Ask a Question