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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-18-2012, 12:39 AM
|
#1
|
Member
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135
Rep:
|
Cut the selected part of directory name
Hello all,
In the shell script, I want to cut a portion of a directory name (which is a symbolic link to another directory)
kaushik@kaushik-desktop:/sys/bus/pci/devices/0000:02:02.0/driver/module/drivers$ ls -alt
total 0
lrwxrwxrwx 1 root root 0 2012-07-18 14:13 pci:ENS1371 -> ../../../bus/pci/drivers/ENS1371
kaushik@kaushik-desktop:/sys/bus/pci/devices/0000:02:02.0/driver/module/drivers$
In the above, I want to get the part "ENS1371" from the full directory name of "pci:ENS1371"
Thanks for your help in advance.
|
|
|
07-18-2012, 12:59 AM
|
#2
|
Member
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135
Original Poster
Rep:
|
I did it by:
echo * | cut -d":" -f2
But is there any other way to do it with awk or sed in a cleaner way?
|
|
|
07-18-2012, 01:36 AM
|
#3
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,250
|
"man basename" ?
|
|
1 members found this post helpful.
|
07-18-2012, 01:48 AM
|
#4
|
Member
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135
Original Poster
Rep:
|
@syg00:
Thanks for your suggestion, but I don't think "basename" is going to help me out.
If you read my first post carefully, you can see that I want "ENS1371" from "pci:ENS1371". Dont think basename strips of when the delimiter is ":"
|
|
|
07-18-2012, 02:05 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018
|
How about :
Code:
cd /sys/bus/pci/devices/0000:02:02.0/driver/module/drivers
file=(*)
echo ${file#*:}
|
|
1 members found this post helpful.
|
07-18-2012, 02:38 AM
|
#6
|
Member
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135
Original Poster
Rep:
|
@grail: Thanks.
I took your solution and ran the below script it works fine.
#!/bin/sh
cd /sys/bus/pci/devices/0000:02:02.0/driver/module/drivers
match="ENS1371"
for file in *;
do
[ $(echo ${file#*:}) = "$match" ] && echo "Matched"
done
|
|
|
07-18-2012, 03:00 AM
|
#7
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,250
|
Let's hope there's only ever one colon (easily fixed I know) ...
And in my own (weak) defence, might I point out that basename should work just fine once readlink has resolved the link.
Nowhere near as elegant as grails - gotta remember that parameter stuff one day ...
|
|
|
07-18-2012, 03:37 AM
|
#8
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,018
|
Firstly, please use [code][/code] tags when displaying code.
Just a word on your test:
1. I assume there are multiple files in the directory as otherwise the for loop is pointless.
2. the echo is not required at all:
Code:
[[ ${file#*:} == $match ]]
|
|
|
All times are GMT -5. The time now is 03:31 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|