Cutting file name from end


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting file name from end
# 1  
Old 02-24-2011
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
# 2  
Old 02-24-2011
try
Code:
$ a=123432311234567890.txt
$ expr $a : '.*\(..........\).txt'
1234567890
$

# 3  
Old 02-24-2011
one way:

Code:
#  echo "123432311234567890.txt" | nawk -F. '{print substr($1,length($1)-9,10)}' 
1234567890

# 4  
Old 02-24-2011
Code:
# a=123432311234567890.txt
# a=${a%.*};t=${a%??????????};echo "${a#$t}"
1234567890

Code:
# echo "123432311234567890.txt" | sed 's/.*\(.\{10\}\)\.txt/\1/'
1234567890

Code:
# a=123432311234567890.txt
# typeset -R10 a=${a%.*}
# echo $a
1234567890


Last edited by ctsgnb; 02-24-2011 at 09:18 AM..
# 5  
Old 02-24-2011
Code:
 var=$(echo "123432311234567890.txt"  | ruby -e 'puts gets.split(/\./)[0..-2].join(".")[-10..-1]')

# 6  
Old 02-25-2011
through sed..
Code:
STR=$(echo "123432311234567890.txt"|sed 's/.*\(.\{10\}\).txt/\1/')

# 7  
Old 02-25-2011
Or...
Code:
STR=$(basename 123432311234567890.txt .txt | tail -c 11)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cutting pattern from source file

hi gurus, I want to cut a selected pattern from the source file For example <operation > Update Update Update <operation> I want to select the three lines having update and the one above <operation> to be cut from source file and paste it to another As of now i... (2 Replies)
Discussion started by: r_t_1601
2 Replies

2. 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

3. Shell Programming and Scripting

Add end of char \n on end of file

Hi, I want to add \n as a EOF at the end of file if it does't exist in a single command. How to do this? when I use command echo "1\n" > a.txt and od -c a.txt 0000000 1 \n \n 0000003 How does it differentiate \n and eof in this case? Regards, Venkat (1 Reply)
Discussion started by: svenkatareddy
1 Replies

4. Shell Programming and Scripting

Cutting columns starting at the end of each line...

Hi Guys, Can you help me with a sed or a csh script that will have an output from the input below. Cutting the columns starting from the end of the line and not from the start of the line? Sample1 - The underscore character "_" is actually a space...i need to put it as underscore here coz... (2 Replies)
Discussion started by: elmer1503
2 Replies

5. Shell Programming and Scripting

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... (7 Replies)
Discussion started by: anushree.a
7 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 Columns and Moving in to a file

Guys, Can any one tell me how can we cut the columns and move each column in to a separate file using awk? I have a tab delimited file as shown below, 1213 wattt werree 2345 skhasdjh aasas I want to output this in to three files named a.txt,b.txt and c.txt say a.txt... (3 Replies)
Discussion started by: Serious Sam
3 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