Thursday, March 23, 2006

unix tip: find a character position in word

somebody asked on how to find a character position in a word/line on the unix.com forum

the question:
how to find a character position in a word/line?
eg.
Unix forum is the best site.

now in above line i want to find the character 'x' position
i.e 4

this is what i came up with:
$ echo "Unix forum is the best site." | grep -o "[-_a-zA-Z0-9\.]" | grep -n x
4:x
update:
one of the users has a very simple solution:
$ expr index "Unix forum is the best site." x
4

3 comments:

dazzling 948 said...

sungguh linux blog ami ini...
miss ko la ami...
hahaa..
takde ko oscc xde seri..
:))

Anonymous said...

bravo!...
ami now become script gurus

Srinivasa kalyan (Smplisri) said...

How to get the same in KSH? Because option -- o is not there in that..