
Extracting Directory Names within a Shell Script
A reader writes:
First, thanks for writing Wicked Cool Shell Scripts. I've found them quite useful and I've learned many things from those scripts. I just have a question and I was wondering if you could point me in the right direction. $ echo /usr/lib/test | cut -d/ -f2 usrYou can drop that into a pipeline and if we assume that your list of files is itself in a file called "filelist", you're halfway there with this one line: cut -d/ -f2 filelistIf you want to prepend the slash, you can do that with sed, but let's tackle the "unique" part first with uniq. Since that particular utliity only works if the input is sorted, here's the pipe I have: cut -d/ -f2 filelist | sort | uniq | sed 's/^/\//'That'll do everything you want and it's short enough to be an alias! Hope that helps and good luck with your script writing efforts.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon
Categorized:
Shell Script Programming
(Article 3731)
Tagged: Previous: What's Acceptable Search Engine "Spam" Technique? Next: Extracting the correct column with "ps" and "awk" Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. Good question, I tried this way: [jsaikia@zorro trap]$ cat dir.names [jsaikia@zorro trap]$ cat dir.names | awk -F "/" '{print "/"$2}' | sort | uniq Hi, I have problem with some shell programing ,would you please help me? thanks alot for your kindly attention Posted by: leila at November 7, 2008 6:15 AMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|