LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-13-2016, 03:58 PM   #1
johnyjj2
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Rep: Reputation: 0
Expr Index - treat space as string, not array separator


Hi,

Code:
    myusername@mycompname:~$ varCurl="abc def curl: ( xyz asdf"
    myusername@mycompname:~$ expr index '$varCurl' "curl: ("
    4
    myusername@mycompname:~$ expr index "$varCurl" "curl: ("
    3
    myusername@mycompname:~$ echo $varCurl
    abc def curl: ( xyz asdf
How to make sure that space is treated as space and string is not treated as array?

Thanks!

Last edited by johnyjj2; 03-13-2016 at 06:18 PM. Reason: Marked as solved
 
Old 03-13-2016, 04:40 PM   #2
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.1 MATE
Posts: 1,906

Rep: Reputation: 617Reputation: 617Reputation: 617Reputation: 617Reputation: 617Reputation: 617
I'm not sure of your question, but the command is expr index STRING CHARS

Code:
expr index '$varCurl' "curl: ("
Because '$varCurl' is in single quotes, it is treated as a literal string. The command looks for any of the characters "c", "u", "r", "l" ":" " " or "(" in the string. It finds the "r" at position 4 (the string starts at position 1).

Code:
expr index "$varCurl" "curl: ("
"$varCurl" refers to the variable $varCurl as it is in double quotes. The command looks for any of the characters, as above, in the string represented by the variable. It finds the "c" at position 3.
 
1 members found this post helpful.
Old 03-13-2016, 05:01 PM   #3
johnyjj2
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks,

abc def curl: ( xyz asdf
123456789

a=1, b=2, c=3, ''=4, d=5, e=6, f=7, ''=8, c=9, u=10 etc. I don't want to find first 'c', but the whole 'curl: ('.

I expect value 9 (or 10 for zero based).

Last edited by johnyjj2; 03-13-2016 at 05:04 PM.
 
Old 03-13-2016, 06:09 PM   #4
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.1 MATE
Posts: 1,906

Rep: Reputation: 617Reputation: 617Reputation: 617Reputation: 617Reputation: 617Reputation: 617
Quote:
Originally Posted by johnyjj2 View Post
Thanks,

abc def curl: ( xyz asdf
123456789

a=1, b=2, c=3, ''=4, d=5, e=6, f=7, ''=8, c=9, u=10 etc. I don't want to find first 'c', but the whole 'curl: ('.

I expect value 9 (or 10 for zero based).
8 for zero-based.

A quick Google for "bash index of substring in string" returns several ways of doing it, none of them as trivial as I would like, but including, for example:

Code:
varCurl="abc def curl: ( xyz asdf"

search="curl: ("

rest=${varCurl#*$search}

echo $((${#varCurl}-${#rest}-${#search}))
 
1 members found this post helpful.
Old 03-13-2016, 06:19 PM   #5
johnyjj2
LQ Newbie
 
Registered: Jul 2009
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks, just marked it as solved . I was looking on Google but somehow different search keywords, thus no results that I found useful. Your solution worked very well .

I'm trying to make function from it but for now it doesn't return anything:

Code:
function contains {
	origStr=$1
	toFindInOrigStr=$2
	rest=${origStr#*$toFindInOrigStr}
	return $((${#origStr}-${#rest}-${#toFindInOrigStr}))
}
Code:
myuser@debian:~$ contains "abcdefgh" "cde"
myuser@debian:~$ echo contains "abcdefgh" "cde"
contains abcdefgh cde
myuser@debian:~$ echo $(contains "abcdefgh" "cde")

myuser@debian:~$
but I'll figure it out sooner or later. [EDIT: Just found that only integers can be returned in bash functions, so it should be doable here. But decided to use global variable and then assign as eval "$3=$valToRet"]

Last edited by johnyjj2; 03-13-2016 at 06:43 PM.
 
Old 03-13-2016, 06:41 PM   #6
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.1 MATE
Posts: 1,906

Rep: Reputation: 617Reputation: 617Reputation: 617Reputation: 617Reputation: 617Reputation: 617
Quote:
Originally Posted by johnyjj2 View Post
Thanks, just marked it as solved . I was looking on Google but somehow different search keywords, thus no results that I found useful. Your solution worked very well .
No problem.
 
Old 03-13-2016, 07:05 PM   #7
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.1 MATE
Posts: 1,906

Rep: Reputation: 617Reputation: 617Reputation: 617Reputation: 617Reputation: 617Reputation: 617
~/bin/contains (chmod +x)

Code:
#!/bin/bash

rest=${1#*$2}
echo $((${#1}-${#rest}-${#2}))
Command line

Code:
myuser@debian:~$ result=$(contains "abcdefgh" "cde")
Remember to handle the eventuality that the string is not found (result<0, I think).

Also ensure that there is no existing command on your system entitled "contains"; if so, use another name that is unique.

The term "contains" would indicate a boolean, whereas you're looking for an index, so reconsider the name anyway.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
reg expr: match a string A-Z, a-z, * or a blank ( but string can not be all blanks) matt007 Programming 4 12-22-2009 09:55 AM
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 06:57 PM
How to multiply with expr? ( expr 3 * 4 doesn't work, though expr 3 + 4 does) lumix Linux - General 5 02-25-2009 06:31 PM
RHEL5 doesn't treat infortrend SCSI disk array as block device Hosferatu Linux - Hardware 3 09-24-2007 11:08 PM
creating array in c++ with string index mohtasham1983 Programming 3 03-11-2007 05:01 PM


All times are GMT -5. The time now is 03:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration