Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 09-26-2005, 02:25 AM   #1
Registered: Sep 2003
Location: Suwon, Korea
Distribution: Slackware 14
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




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
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
There are LOTS of ways to do it. Here's (arguably) the easiest:
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.


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

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