-   Linux - Newbie (
-   -   Cut the selected part of directory name (

kauuttt 07-18-2012 12:39 AM

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

In the above, I want to get the part "ENS1371" from the full directory name of "pci:ENS1371"

Thanks for your help in advance.

kauuttt 07-18-2012 12:59 AM

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?

syg00 07-18-2012 01:36 AM

"man basename" ?

kauuttt 07-18-2012 01:48 AM

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 ":"

grail 07-18-2012 02:05 AM

How about:

cd /sys/bus/pci/devices/0000:02:02.0/driver/module/drivers


echo ${file#*:}

kauuttt 07-18-2012 02:38 AM

@grail: Thanks.

I took your solution and ran the below script it works fine.

cd /sys/bus/pci/devices/0000:02:02.0/driver/module/drivers
for file in *;
[ $(echo ${file#*:}) = "$match" ] && echo "Matched"

syg00 07-18-2012 03:00 AM

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 ...

grail 07-18-2012 03:37 AM

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:

[[ ${file#*:} == $match ]]

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