LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 07-18-2012, 12:39 AM   #1
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Rep: Reputation: 26
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.
 
Old 07-18-2012, 12:59 AM   #2
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Original Poster
Rep: Reputation: 26
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?
 
Old 07-18-2012, 01:36 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,139

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
"man basename" ?
 
1 members found this post helpful.
Old 07-18-2012, 01:48 AM   #4
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Original Poster
Rep: Reputation: 26
@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 ":"
 
Old 07-18-2012, 02:05 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
How about:
Code:
cd /sys/bus/pci/devices/0000:02:02.0/driver/module/drivers

file=(*)

echo ${file#*:}
 
1 members found this post helpful.
Old 07-18-2012, 02:38 AM   #6
kauuttt
Member
 
Registered: Dec 2008
Location: Atlanta, GA, USA
Distribution: Ubuntu
Posts: 135

Original Poster
Rep: Reputation: 26
@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
 
Old 07-18-2012, 03:00 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,139

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
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 ...
 
Old 07-18-2012, 03:37 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
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 ]]
 
  


Reply



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
cut part of a string using awk m4rtin Programming 2 09-03-2009 07:32 PM
How to add subtitles, cut part of a movie and create a DVD wastingtime Linux - Software 0 06-11-2009 06:42 PM
Part of screen cut off moo-cow Ubuntu 4 05-03-2006 05:49 PM
Backing up selected directory... CRCool75 Linux - Enterprise 26 07-23-2004 09:01 PM
Starting terminal from nautilus at selected directory ericwsf Linux - General 0 04-14-2004 04:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:04 AM.

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