grep for a last word from a path


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep for a last word from a path
# 1  
Old 02-04-2010
grep for a last word from a path

Hi ,
I want to get the last word from a path.

Example::
I have path::
/TEMP_REGRESSION/regression/TESTSUITE/TestCases/Test1201/ext_libs/DATA/INTERNAL/pad_ext.lef

I want the last word only.ie pad_ext.lef

Please let me know how to grep that word.
Thanks in advance
# 2  
Old 02-04-2010
Hi.

Code:
$ X=/TEMP_REGRESSION/regression/TESTSUITE/TestCases/Test1201/ext_libs/DATA/INTERNAL/pad_ext.lef
$ echo ${X##*/}
pad_ext.lef


Last edited by Scott; 02-04-2010 at 10:25 AM..
# 3  
Old 02-04-2010
Quote:
Originally Posted by scottn
Hi.

This has been asked many times. You could search the forums.

Code:
$ X=/TEMP_REGRESSION/regression/TESTSUITE/TestCases/Test1201/ext_libs/DATA/INTERNAL/pad_ext.lef
$ echo ${X##*/}
pad_ext.lef


Hi Scott,
Thanks a lot for your fast replySmilie
Regards,
Neelam G
# 4  
Old 02-04-2010
Or with awk:

Code:
X="/TEMP_REGRESSION/regression/TESTSUITE/TestCases/Test1201/ext_libs/DATA/INTERNAL/pad_ext.lef"

echo $X | awk -F'/' '{print $NF}'

# 5  
Old 02-04-2010
Why use awk when a simple basename is sufficient?
# 6  
Old 02-04-2010
MySQL

Because I didn't know about the basename command? Smilie

But you're right, it's the best for this task, since this is what it's designed to do.

Thanks for the heads up.
# 7  
Old 02-23-2010
MySQL

The following ways we can achieve your requirement.


1. Using cut

Code:
cut -d '/' -f 10 path

2.1 Using grep

Code:
grep --color '/\([^/]*\)$' file name

Output:

/TEMP_REGRESSION/regression/TESTSUITE/TestCases/Test1201/ext_libs/DATA/INTERNAL/pad_ext.lef

2.2 Using grep
Code:
grep --color -o '\([^/]*\)$' filename

Output:
pad_ext.lef
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep for a word or word with underscore

I have a file "test" with following contents: cat test abc abcd_efg abc_abc I want to only grep for abc or abc_ without getting other results, how do I achieve this? If I use grep -w abc test option I get only abc and not abc_. If I use egrep "abc|abc_" test its still printing... (3 Replies)
Discussion started by: ctrld
3 Replies

2. Shell Programming and Scripting

Need a word which just comes next to after grep of a specific word

Hi, Below is an example : ST1 PREF: int1 AVAIL: int2 ST2 PREF :int1 AVAIL: int2 I need int1 to come in preferred variable while programming and int2 in available variable Please help me doing so Best regards, Vishal (10 Replies)
Discussion started by: Vishal_dba
10 Replies

3. Shell Programming and Scripting

Need a perl script similiar to grep -r 'word' /path/to/dir"

Hi , i am looking for a perl script to grep for a string in all files inside a directory . bash command . grep -r 'word' /path/to/dir Thanks, Nvil (3 Replies)
Discussion started by: nevil
3 Replies

4. Shell Programming and Scripting

How ti Grep for a word and print the next word

Hi can we grep for a word and print the next word of the greped word? ex:- create or replace function function_name create function function_name we should search for word "function" and output next word "function_name" from both lines. (3 Replies)
Discussion started by: manasa_vs
3 Replies

5. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

6. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

How to grep a word and next column to that word?

Hi, I have input file as below. Can you help me? inac_4y;0;2;Balance;200;1;1; 0;2;Balance;100;1; 0;inac_nq;0;1;Balance;100;1 desired output Balance;200 Balance;100 Balance;100 -Suresh Please use and tags when posting code, data or logs etc. to preserve formatting... (5 Replies)
Discussion started by: suresh3566
5 Replies

8. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. UNIX for Dummies Questions & Answers

how to grep the word and display only the second word from it

hi, consider the below line in a text file, 'Y',getdate(),'N','V',NULL ..... 'N',getdate(),'Y','D',NULL ..... 'Y','N','Y',getdate(),'Y','D',NULL .... as u see above, i want only the second word after the getdate() word... getdate() will not come 2nd word alwys it may be any position but i... (11 Replies)
Discussion started by: prsam
11 Replies

10. UNIX for Dummies Questions & Answers

how to grep for a word and display only the word

Hi, When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Ananth (6 Replies)
Discussion started by: ananthmm
6 Replies
Login or Register to Ask a Question