FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Ubuntu > Ubuntu User

 
 
LinkBack Thread Tools
 
Old 11-29-2009, 01:09 PM
Ray Parrish
 
Default Finding the position of a sub string within a strong

Hello,

I am starting to get a grasp on what can be done with regular
expressions, and I now know how to extract a sub string from within
another string if I know it's offset, and length.

The thing I haven't discovered during all of this reading is a way to
return the position of a substring within another string.

How would I return the position of the sub string "zat" in the string
"abczat1256"?

I know how to get the length of a string now, but not how to search
within it yet. I have thought of a cludge which involves stripping
character at a time from the front of the string into an array, but why
re-invent the wheel when I'm sure there is already a simpler way to do this.

Thanks for any help you can be.

Later, Ray Parrish

--
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 01:12 PM
Loïc Grenié
 
Default Finding the position of a sub string within a strong

2009/11/29 Ray Parrish <crp@cmc.net>:
> Hello,
>
> I am starting to get a grasp on what can be done with regular
> expressions, and I now know how to extract a sub string from within
> another string if I know it's offset, and length.
>
> The thing I haven't discovered during all of this reading is a way to
> return the position of a substring within another string.
>
> How would I return the position of the sub string "zat" in the string
> "abczat1256"?
>
> I know how to get the length of a string now, but not how to search
> within it yet. I have thought of a cludge which involves stripping
> character at a time from the front of the string into an array, but why
> re-invent the wheel when I'm sure there is already a simpler way to do this.

Do you really need to know the position of the string ? Many things
can be done without knowing the position of the substring (substituting
it, for instance).

Loïc

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 05:42 PM
James Michael Fultz
 
Default Finding the position of a sub string within a strong

* Ray Parrish <crp@cmc.net> [2009-11-29 06:09 -0800]:
> I am starting to get a grasp on what can be done with regular
> expressions, and I now know how to extract a sub string from within
> another string if I know it's offset, and length.

Using regular expressions allows for matching patterns where you may not
know exact offset and length.

> The thing I haven't discovered during all of this reading is a way to
> return the position of a substring within another string.
>
> How would I return the position of the sub string "zat" in the string
> "abczat1256"?

$ echo "abczat1256" | awk '{print index($0,"zat"}'

> I know how to get the length of a string now, but not how to search
> within it yet. I have thought of a cludge which involves stripping
> character at a time from the front of the string into an array, but why
> re-invent the wheel when I'm sure there is already a simpler way to do this.

You can perform some basic string manipulations in the shell itself,
particularly using Bash (or Ksh or Zsh). You can perform more complex
string manipulations with sed and AWK.

sed is good for string replacements within a line. It's not so good for
multi-line string manipulations. You can do more than string
replacement with sed, but it gets complicated quickly.

AWK can perform all sorts of text manipulations as well as arithematic
and is preferable sed for multi-line operations.

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-29-2009, 08:02 PM
Ray Parrish
 
Default Finding the position of a sub string within a strong

James Michael Fultz wrote:
> * Ray Parrish <crp@cmc.net> [2009-11-29 06:09 -0800]:
>
>> I am starting to get a grasp on what can be done with regular
>> expressions, and I now know how to extract a sub string from within
>> another string if I know it's offset, and length.
>>
>
> Using regular expressions allows for matching patterns where you may not
> know exact offset and length.
>
>
>> The thing I haven't discovered during all of this reading is a way to
>> return the position of a substring within another string.
>>
>> How would I return the position of the sub string "zat" in the string
>> "abczat1256"?
>>
>
> $ echo "abczat1256" | awk '{print index($0,"zat"}'
>
>
>> I know how to get the length of a string now, but not how to search
>> within it yet. I have thought of a cludge which involves stripping
>> character at a time from the front of the string into an array, but why
>> re-invent the wheel when I'm sure there is already a simpler way to do this.
>>
>
> You can perform some basic string manipulations in the shell itself,
> particularly using Bash (or Ksh or Zsh). You can perform more complex
> string manipulations with sed and AWK.
>
> sed is good for string replacements within a line. It's not so good for
> multi-line string manipulations. You can do more than string
> replacement with sed, but it gets complicated quickly.
>
> AWK can perform all sorts of text manipulations as well as arithematic
> and is preferable sed for multi-line operations.
>
Hello,

While reading i found the following method, which seems reasonable to me.

Position=`expr index "$String" "$SubString"`

That returns the starting position of SubString within String, and it
even works! I'm beginning to see the possibilities as I learn more about
bash.

Thanks for the awk pointer, but this method assigns directly to a
variable, which is what I was looking for.

Later, Ray Parrish


--
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com



--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 
Old 11-30-2009, 02:13 AM
James Michael Fultz
 
Default Finding the position of a sub string within a strong

* Ray Parrish <crp@cmc.net> [2009-11-29 13:02 -0800]:
> While reading i found the following method, which seems reasonable to me.
>
> Position=`expr index "$String" "$SubString"`
>
> That returns the starting position of SubString within String, and it
> even works! I'm beginning to see the possibilities as I learn more about
> bash.

Except it doesn't do that unless you have a very different
implementation of expr than I do here. It returns the index of the
first matching character of the second argument in the first argument.
The syntax is properly described as 'expr index STRING CHARACTERS'.

$ expr index abczat1256 zat
1

The first matching character being the 'a' at position 1 in the string.

Also, it's notable that 'index' may be a GNU extension to expr since
I did not find it available on NetBSD.

> Thanks for the awk pointer, but this method assigns directly to a
> variable, which is what I was looking for.

Any command output can be captured in a variable, so you may use AWK or
another program if needed.

Position=`echo "$String" |
awk -v SubString=$SubString" '{print index($0,SubString)}'`

--
ubuntu-users mailing list
ubuntu-users@lists.ubuntu.com
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
 

Thread Tools




All times are GMT. The time now is 09:12 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org