|
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.
Categorized:
Shell Script Programming
(Article 3731,
Written by Dave Taylor)
Tagged: Previous: What's Acceptable Search Engine "Spam" Technique? Next: Extracting the correct column with "ps" and "awk" Subscribe!
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 AMExtracting Directory Names within a Shell Script can be done using the following command [janeesh@jansworld.in usr]# find /usr -type d -maxdepth 1 This will list all the directories in /usr folder. Posted by: Janeesh at June 9, 2010 10:45 PMI have something to say, now that you mention it, but ...
I do have a comment, now that you mention it!
|
Recommended
Recent Entries
Search
I Need Help!
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and RSS Feeds Building Web Site Traffic Business and Management CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above Facebook Help Google Plus Help HTML and CSS Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Mac OS X Help Pay Per Click (PPC) Advertising Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help WordPress Help |