LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-26-2005, 02:25 AM   #1
czarherr
Member
 
Registered: Sep 2003
Location: Suwon, Korea
Distribution: Slackware 13
Posts: 288

Rep: Reputation: 32
basic question about grep


Sorry for the noob question here, I'm just getting started with scripting, and I need a simple result. I just want grep to take a directory path from $1 and cut the directory name off the end of it in the script. So

/home/user/directory

becomes

directory

I have tried using grep --count "/" $1 | cut -d/ -f$variabletodeterminefield

but I always get either 0 or 1 returned. Any help would be greatly appreciated.
 
Old 09-26-2005, 02:46 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
There are LOTS of ways to do it. Here's (arguably) the easiest:
Code:
last_component=$( basename ${variabletodeterminefield} )
directory_component=$( dirname ${variabletodeterminefield} )
If the variable contains "/home/czarherr" then last_component is "czarherr" and directory_component is "/home". As another example, if the variable was "/usr/local/share/themes/" then last_component is "themes" and directory_component is "/usr/local/share"

There are ways to do the same thing with sed, awk, and others, but this is probably what you're looing for.
 
  


Reply


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
grep question djgerbavore Linux - Newbie 3 11-04-2004 10:53 AM
Question about Grep irfanhab Linux - Newbie 6 08-23-2004 02:57 PM
grep question djgerbavore Linux - Newbie 7 07-07-2004 07:11 AM
grep question Hockeyfan Linux - General 1 05-15-2004 02:09 AM
grep question keyops Programming 2 04-09-2004 09:17 PM


All times are GMT -5. The time now is 03:39 AM.

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