LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-14-2011, 02:28 PM   #1
nsingh49
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Rep: Reputation: 0
String Manipulation question!


I have a string and keeping the delimiter to be /, I would like extract highlighted string

/u01/oradata/mydatabase/ABCF.dbf

I can extract the last field after the delimiter, but all the fields before is the challenge to me.

Thanks

Nirmal
 
Old 02-14-2011, 02:44 PM   #2
nsingh49
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Ok I figured out

basename /u01/oradata/mydatabase/ABCF.dbf

gives me ABCF.dbf

dirname /u01/oradata/mydatabase/ABCF.dbf

gives me /u01/oradata/mydatabase


Cheers...
 
Old 02-14-2011, 02:49 PM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian
Posts: 1,265

Rep: Reputation: 98
Try this.

Code:
echo "/u01/oradata/mydatabase/ABCF.dbf" | awk -F"/" '{print "/"$1"/"$2"/"$3}'
In future post requests like these in Programming Forum.
 
Old 02-15-2011, 12:43 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874Reputation: 1874
Or if you actually want to use string manipulation in bash:
Code:
LINE="/u01/oradata/mydatabase/ABCF.dbf"
echo ${LINE##*/}    # this gives you filename
echo ${LINE%/*}     # this gives you path to file
 
Old 02-15-2011, 08:32 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Or sed:

Code:
sed 's/^\(.*\)/[^/]*$/\1/'
But if you're dealing with actual paths to files, you should really use basename and dirname.

If the issue is solved, mark the thread as solved.
 
Old 02-15-2011, 09:08 AM   #6
nsingh49
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Vikas, you suggested:
echo "/u01/oradata/mydatabase/ABCF.dbf" | awk -F"/" '{print "/"$1"/"$2"/"$3}'

In my case I don't know how many fields are there. It would be 3,4 or 5 and who know.

Thanks

Nirmal
 
Old 02-15-2011, 12:44 PM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by MTK358 View Post
But if you're dealing with actual paths to files, you should really use basename and dirname.
Hi MTK358 Does that mean there is some reason not to use grail's suggestions? I use them exclusively (never basename or dirname) and have not yet encountered a problem.
 
Old 02-15-2011, 01:02 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by catkin View Post
Hi MTK358 Does that mean there is some reason not to use grail's suggestions? I use them exclusively (never basename or dirname) and have not yet encountered a problem.
I guess they would work fine, but if they provide you with utilities just for getting the filename and directory name from a path, why not use them? It seems like it would be more readable, too.
 
  


Reply

Tags
awk, sed, shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation C++ miamagoo Programming 6 07-31-2010 12:55 PM
string manipulation jtag Linux - General 9 02-01-2010 08:07 AM
String manipulation in C xeon123 Programming 2 10-17-2007 01:17 PM
String manipulation Manuel Alvarado Linux - Newbie 2 09-06-2006 09:49 PM
string manipulation mannahazarika Programming 6 12-31-2005 11:02 AM


All times are GMT -5. The time now is 06:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration