TO Extract Last 10 Characters from a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting TO Extract Last 10 Characters from a line
# 1  
Old 10-24-2008
TO Extract Last 10 Characters from a line

Hi Guys!!!!!!!!!!!

How do we extract last 13 characters from a line

I/p:

.R1/new/sv902a.01.per

O/p:
sv902a.01.per

And the no of characters differs in each line..

Kindly help me out in this!!!!!!!!!!!

Thanks in Advance
# 2  
Old 10-24-2008
To print the last 13 characters of a line:

Code:
echo "$string" | awk '{print substr($0,length-12)}'

To get the last characters after the slash:

Code:
echo ${string##*/}

with basename:

Code:
basename "$string"

or with sed:

Code:
echo "$string" | sed 's_.*/__'

Regards
# 3  
Old 10-24-2008
Code:
echo ".R1/new/sv902a.01.per" | sed 's/\(.*\)\(.\{13\}\)$/\2/'

# 4  
Old 10-24-2008
summer_cherry has a nice solution but lacks readability.
just to prove there is more than one sulution

get the last 13 characters:
echo ".R1/new/sv902a.01.per" | rev | cut -b-13| rev

get the filename:
FOO=".R1/new/sv902a.01.per"
echo ${FOO##*/*/}
# 5  
Old 10-24-2008
Quote:
Originally Posted by grumpf
echo ${FOO##*/*/}
Shorter Smilie
Code:
echo ${FOO##*/}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract characters from a string name

Hi All, I am trying to extract only characters from a string value eg: abcdedg1234.cnf How can I extract only characters abcdedg and assign to a variable. Please help. Thanks (2 Replies)
Discussion started by: abhi_123
2 Replies

2. Shell Programming and Scripting

Extract 4 digit characters

* hdisk99 U5791.001.9920BZ4-P1-C05-T1-W500507630E060C14-L401140BA00000000 IBM MPIO FC 1750 * hdisk100 U5791.001.9920BZ4-P1-C05-T1-W500507630E060C14-L401140BB00000000 IBM MPIO FC 1750 * hdisk185 U5791.001.9920BZ4-P1-C05-T1-W500507630E060C14-L401140A000000000 IBM MPIO FC... (2 Replies)
Discussion started by: Daniel Gate
2 Replies

3. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

4. Shell Programming and Scripting

extract last 8 characters from a file name

Dear gurus I have several files with the following format filenameCCYYMMDD , that is the last 8 characters will be the date in CCYYMMDD format. eg FILENAME20110523 . Could anyone please put me through on how to extract only the last 8 characters from the files. I am thinking of using awk,sed... (2 Replies)
Discussion started by: erinlomo
2 Replies

5. Shell Programming and Scripting

Extract data between two characters

Hi, how can i extract a data between two characters input file is abc efg hig - 99.4% (16337276 kB) used efg klm - 47.1% (7734968 kB) used hij - 99.4% (16341464 kB) used klm -93.7% (15394516 kB) used output should be 99.4 (5 Replies)
Discussion started by: jobycxa
5 Replies

6. UNIX for Dummies Questions & Answers

extract characters from a variable

Hi All, I have scenario as below, I want to cut the characters and store it variable2.. and want to display the variable2.. as shown below... eg: the size may differs , i am show just an example..... numbers are not fixed in variable1..i want to extract characters and... (1 Reply)
Discussion started by: G.K.K
1 Replies

7. Shell Programming and Scripting

need to extract field of characters in a line

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could you please let me know the shell script... (3 Replies)
Discussion started by: jansat
3 Replies

8. Shell Programming and Scripting

SED to extract characters

Hi, Please let me know how do we write a piece of code to extract characters from a line using SED. (1 Reply)
Discussion started by: Shilpi
1 Replies

9. Shell Programming and Scripting

SED to extract characters

Hi, Please let me know how do we write a piece of code to extract characters from a line using SED. (2 Replies)
Discussion started by: Shilpi
2 Replies

10. UNIX for Dummies Questions & Answers

Grep and extract the characters after

Hi All, I have lines like below in a file A /u/ab/test1.dat A/u/ab/test2.dat A /u/bb/test3.dat A/u/cc/test4.dat I will need /u/ab/test1.dat /u/ab/test2.dat /u/bb/test3.dat /u/cc/test4.dat Pls help Thanks (6 Replies)
Discussion started by: thumsup9
6 Replies
Login or Register to Ask a Question