LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 09-17-2008, 05:05 PM   #1
ptml
LQ Newbie
 
Registered: Sep 2008
Posts: 3

Rep: Reputation: 0
BASH string manipulation help


The expression:

expr match "now is the the time" ".*the"

Returns 14, the "e" in the second "the"

What is the expression to return the position of the FIRST "the" (any character is fine).

Thanks,
Paul
 
Old 09-17-2008, 07:01 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,561
Blog Entries: 54

Rep: Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927Reputation: 2927
"expr index"?: i="now is the the time"; n=`expr index "now is ze the time" "t"`; echo "${i:${n}:1} -> $n"
 
Old 09-18-2008, 12:43 AM   #3
ptml
LQ Newbie
 
Registered: Sep 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Ya, I went down that road and found that is great for finding the first instance of a single letter, but doesn't work for a string search. Thanks for taking a shot at it!

Cheers,
Paul
 
Old 09-18-2008, 12:55 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
# s="now is the the time"
# echo ${s%%e*}
now is th
# s=${s%%e*}
# echo "Index of  first e is $(( ${#s} + 1 ))"
Index of  first e is 10
 
Old 09-18-2008, 03:00 AM   #5
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Rep: Reputation: 15
Quote:
Originally Posted by ptml View Post
The expression:

expr match "now is the the time" ".*the"

Returns 14, the "e" in the second "the"

What is the expression to return the position of the FIRST "the" (any character is fine).

Thanks,
Paul
expr match will return the no of letters in the matched string that why your substring ".*the" returns the no of characters till "the" that is 14.

use this
code:
Code:
echo `expr index "now is the the time" "[a-z]*the"`
it will return 8 as the first occurrence of 't' is at 8
 
Old 09-19-2008, 11:14 AM   #6
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 330

Rep: Reputation: 140Reputation: 140
Reversing the line overcomes the limitation of expr to matching strings from the start of the line:-
Code:
r=$(echo "now is the the time" | rev)  # Gives "emit eht eht si won"
p=$(expr match "$r" ".*\beht\b")
if [ $p -eq 0 ];then
  echo 0
else
  expr length "$r" + 1 - $p
fi
Using sed is better:-
Code:
expr length "$(echo "now is the the time" | sed -n 's/\bthe\b.*/t/p')"
The first 'the' to the end of the line is replaced with 't'.
\b represents a word boundary (to avoid matches with words like 'then')
-n is so that 0 is returned if there isn't a 'the' in the line.

EDIT: A shorter version of the first method
Code:
r=$(echo "now is the the time" | rev)
expr length match "$r" ".*\beh\(t\b.*\)"

Last edited by Kenhelm; 09-19-2008 at 11:58 AM.
 
Old 09-20-2008, 12:21 AM   #7
ptml
LQ Newbie
 
Registered: Sep 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Bravo. Never in a million years would I have thought of reversing the string. Makes perfect sense. What would make more sense is for "expr" to include a flag to seek the first occurrence of something! Thanks for your insight.
 
Old 08-26-2010, 05:58 PM   #8
philluder
LQ Newbie
 
Registered: Aug 2010
Posts: 3

Rep: Reputation: 0
string manipulation

If you use bash 4.x you can source the oobash. A string lib written in bash with oo-style. You only need to source the file...and running. There is a help system embedded, so you can ask for the description of every "method" :

http://sourceforge.net/projects/oobash/

String is the constructor function:

>String a abcda

>a.indexOf a

>0

>a.lastIndexOf a

>4

>a.indexOf da

>3

There are many "methods" more to work with strings in your scripts:

-base64Decode
-base64Encode
-capitalize
-center
-charAt
-concat
-contains
-count
-endsWith
-equals
-equalsIgnoreCase
-reverse
-hashCode
-indexOf
-isAlnum
-isAlpha
-isAscii
-isDigit
-isEmpty
-isHexDigit
-isLowerCase
-isSpace
-isPrintable
-isUpperCase
-isVisible
-lastIndexOf
-length
-matches
-replaceAll
-replaceFirst
-startsWith
-substring
-swapCase
-toLowerCase
-toString
-toUpperCase
-trim
-zfill
 
Old 08-26-2010, 07:38 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Alternatively, using parameter substitution
Code:
c@CW9:~$ string='now is the time'
c@CW9:~$ buf=the${string#*the}
c@CW9:~$ echo $(( ${#string} - ${#buf} + 1 ))
 
  


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
String manipulation in bash dalmat Linux - Software 4 12-03-2007 06:50 PM
bash string manipulation kpachopoulos Programming 3 06-17-2007 07:45 PM
string manipulation in BASH ovince Programming 4 04-16-2007 08:15 PM
String manipulation with BASH script King V Programming 9 04-21-2006 04:15 PM
bash + string manipulation dave bean Programming 7 02-16-2005 12:16 PM


All times are GMT -5. The time now is 07:44 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration