LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2008, 07:20 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
Inspected a character on a particular position in a word


I have a variable in my shell script, say $DEVICE, which contains the following string:
Code:
/dev/scd0
Now here the letter '0' could be anything like:
Code:
"0" in /dev/scd0
"1" in /dev/scd1
"2" in /dev/scd2
and so on...

Now I would like to inspect which letter is there after the string /dev/scd?

So for example what is there at the position "*" in the string /dev/scd*

Thanks
 
Old 07-02-2008, 07:40 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
echo $DEVICE | sed 's-^/dev/scd--'

will strip all but the trailing device number. Assign that to a variable:

DEVNO=$(echo $DEVICE | sed 's-^/dev/scd--')

You can also use shell variable string manipulation as well, but the above is easier perhaps for you to initially understand.
 
Old 07-02-2008, 07:51 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
dev=/dev/scd2
echo $dev|cut -c9
2
 
Old 07-02-2008, 08:06 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Or perhaps better yet:

Code:
echo /dev/scd26 | perl -lne 'print $1 if /(\d+)$/'
26
in case the device has more than 9 units.
 
Old 07-02-2008, 08:58 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
I think in this case shell variable string manipulation is pretty clear (assuming bash):
Code:
~/tmp$ echo $DEVICE
/dev/scd26
~/tmp$ echo ${DEVICE#/dev/scd}
26
 
Old 07-02-2008, 09:23 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
The problem I personally have with parameter expansion for variables is that it is generally limited and certainly non-intuitive for new users. And once you need slightly more complex patterns, it becomes unwieldy. It uses only a limited form of filename patterns, and doesn't behave as one might expect:

Code:
$ DISK=/dev/sda22
$ echo ${DISK#/dev/*a}
22
$ echo ${DISK#/dev/[a-z]}             # OK, a-z char range matches
da22
$ echo ${DISK#/dev/[a-z]*}            # What!  Ok, maybe longer form...
da22
$ echo ${DISK##/dev/[a-z]*}           # Hmmm, that's not what I want either

$ echo ${DISK#/dev/[a-z][a-z][a-z]}   # Good, but forces 3 chars in pattern
22
$ echo ${DISK#/dev/[a-z][a-z][a-z]*}  # What!  Same as above.
22
$ echo ${DISK##/dev/[a-z][a-z][a-z]*} # Nope.  How do I say "all but last n digits" ?

$
And more complex patterns are even more heinous.
 
Old 07-02-2008, 09:29 PM   #7
andymalato
LQ Newbie
 
Registered: Jun 2006
Distribution: Red Hat, Solaris, FreeBSD
Posts: 20

Rep: Reputation: 0
Code:
echo $DEVICE
/dev/sda2
echo ${DEVICE:8:4}
2
 
Old 07-02-2008, 09:46 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
... but I think one shouldn't have to think hard, worrying about searching the code for finding various oddities like :8:4, which are very specific to only certain devices types. For example, the solution offered doesn't work for devices with varying pattern lengths. Assume we want to find the unit number for either raidNN or sdaNN:

Code:
$ DISK=/dev/raid20
$ echo ${DISK:8:4}   # Oops, won't work.
d20
 
Old 07-03-2008, 04:25 PM   #9
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Two more methods:
To return only the part of the line which matches the search pattern:-
Code:
echo /dev/raid20 | grep -o '[0-9]*$'
20
To delete the unwanted characters:-
Code:
echo /dev/raid20 | tr -d '[:alpha:][:punct:]'
20
(but used on '/dev/fd0d360' this outputs '0360' which may or may not be what is wanted.)
 
Old 07-03-2008, 04:53 PM   #10
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Who says computer people are not creative!? :-)

Good stuff all.
 
Old 07-03-2008, 08:37 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
# DEVICE=/dev/scd1
# expr "$DEVICE" : '.*scd\(.\)'
1
 
  


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
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 09:46 PM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 06:36 AM
about wide character and multiple byte character George2 Programming 5 05-23-2006 01:03 AM
Java: Change first character in word to upper AMMullan Programming 4 04-05-2004 03:16 PM
function won ' t return character array word Linh Programming 1 07-31-2003 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:25 PM.

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