The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help for shell script of finding shortest substring from given string by user pankajd Shell Programming and Scripting 1 11-22-2007 12:27 PM
Using Awk in shell script to extract an index of a substring from a parent string sandeepms17 Shell Programming and Scripting 2 11-07-2007 07:44 AM
Substring Function sasikumar_l Shell Programming and Scripting 1 02-23-2007 12:25 PM
Substring function in UNIX shell script csrazdan UNIX for Dummies Questions & Answers 5 08-12-2006 04:07 PM
Substring function in UNIX shell script csrazdan Shell Programming and Scripting 1 08-11-2006 06:15 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-18-2009
anilvvnn anilvvnn is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 3
How to do String manipulations using Substring function in Shell

Hi,

I have a scenario to just plug out the file name from the following location path.

/opt/project/data/int/holdFiles/csv195687.csv

So, how do I get just file name which is "csv195687.csv" from the above line using awk/shell scripting? Can we use indexOf and Substring in awk to get last indexOf "/" and then end of String to get file name?

Thanks.
  #2 (permalink)  
Old 08-18-2009
malcomex999 malcomex999 is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 151
if u made a little search, you could have get what u wanted by urself....

Code:
 
str="/opt/project/data/int/holdFiles/csv195687.csv"
echo ${str##*/}
csv195687.csv
  #3 (permalink)  
Old 08-18-2009
anilvvnn anilvvnn is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 3
Thanks for quick reply. It is a cool answer. From next time onwards, I will definitely search the forum before I ask the question.
  #4 (permalink)  
Old 08-18-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 519
Code:
$
$ str="/opt/project/data/int/holdFiles/csv195687.csv"
$ basename $str
csv195687.csv
$
$ echo $str | sed 's/.*\///'
csv195687.csv
$
$ echo $str | awk -F/ '{print $NF}'
csv195687.csv
$
$ echo $str | perl -lne '/.*\/(.*)/ && print $1'
csv195687.csv
$
tyler_durden
  #5 (permalink)  
Old 08-18-2009
anilvvnn anilvvnn is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 3
Thanks Tyler. Hmmm...lot of options.
  #6 (permalink)  
Old 2 Weeks Ago
leobsd leobsd is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 1
basename

str="/opt/project/data/int/holdFiles/csv195687.csv"
basename $str
  #7 (permalink)  
Old 2 Weeks Ago
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by anilvvnn View Post
Hmmm...lot of options.

The only one that makes sense for dealing with a string is:

Code:
echo ${str##*/}
Save external commands for dealing with files.
Reply

Bookmarks

Tags
awk, indexof, shell, substring

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:16 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0