The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to split a value according to character position michaeltravisuk UNIX for Dummies Questions & Answers 4 05-27-2008 04:07 AM
search in finding position of a string in avariable smr_rashmy Shell Programming and Scripting 2 02-08-2008 07:58 PM
Counting position of a character rochitsharma UNIX for Advanced & Expert Users 3 11-27-2007 03:26 PM
Sorting a flat file based on multiple colums(using character position) cucubird Shell Programming and Scripting 8 07-24-2006 09:47 PM
Character position akrathi UNIX for Dummies Questions & Answers 4 10-26-2005 01:06 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-22-2006
Registered User
 

Join Date: Mar 2006
Posts: 35
Finding character position in file

Hi all,

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

As in oracle we can make use of 'instr' function to do this.
Is their any thing in Unix shell script ?

Thanks In advance.
Reply With Quote
Forum Sponsor
  #2  
Old 03-22-2006
Registered User
 

Join Date: Nov 2005
Location: cyberjaya
Posts: 85
my way of doing it:

$ echo "Unix forum is the best site." | grep -o "[-_a-zA-Z0-9\.]" | grep -n x
Reply With Quote
  #3  
Old 03-22-2006
Registered User
 

Join Date: Mar 2006
Posts: 35
Thanks

but it give output as 4:x

I nt it to be 4
Reply With Quote
  #4  
Old 03-22-2006
Registered User
 

Join Date: Nov 2005
Location: cyberjaya
Posts: 85
$ echo "Unix forum is the best site." | grep -o "[-_a-zA-Z0-9\.]" | grep -n x | cut -d: -f1
Reply With Quote
  #5  
Old 03-23-2006
Registered User
 

Join Date: Mar 2006
Posts: 35
Thanks a lot

Quote:
Originally Posted by ashterix
$ echo "Unix forum is the best site." | grep -o "[-_a-zA-Z0-9\.]" | grep -n x | cut -d: -f1
Thanks a lot
Reply With Quote
  #6  
Old 03-23-2006
Registered User
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
Or use the expr command with 'index' :

To return the first position of where any characters in the string "de" is found in "abcdef", enter:

expr index abcdef de

The following is displayed:
4

cheers
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0