How to find the last occurrence of string | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

How to find the last occurrence of string

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-18-2009
padmisri padmisri is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 22 December 2009, 6:58 AM EST
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
How to find the last occurrence of string

Hi,

For example if i have a string VAR="/usr/sample.abc.jks.jks" , how can i check if the last part of the string has .jks?

Thanks ,
Padmini
Sponsored Links
    #2  
Old 08-18-2009
cola cola is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 6 October 2014, 9:41 PM EDT
Location: Dhaka,Bangladesh
Posts: 185
Thanks: 38
Thanked 3 Times in 3 Posts
Quote:
Originally Posted by padmisri View Post
Hi,

For example if i have a string VAR="/usr/sample.abc.jks.jks" , how can i check if the last part of the string has .jks?

Thanks ,
Padmini
Are you trying to tokenize the string with the dot (.)?
Sponsored Links
    #3  
Old 08-18-2009
padmisri padmisri is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 22 December 2009, 6:58 AM EST
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

i just want to check if the string ends with .jks.How can i do that?
    #4  
Old 08-18-2009
malcomex999 malcomex999 is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 8 December 2013, 3:25 AM EST
Location: In the beautiful World...
Posts: 509
Thanks: 9
Thanked 28 Times in 28 Posts
i hope u can use this as steppin stone...

it gives no output ...

Code:
goldprod/test>VAR="/usr/sample.jks.abc"
goldprod/test>echo $VAR | sed -n '/.jks$/p'

it gives output cos last one is ".jks"...

Code:
goldprod/test>VAR="/usr/sample.abc.jks.jks"
goldprod/test>echo $VAR | sed -n '/.jks$/p'
/usr/sample.abc.jks.jks

Sponsored Links
    #5  
Old 08-18-2009
necroman08 necroman08 is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 27 December 2010, 11:16 AM EST
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
 
#!/bin/bash
strin="/usr/sample.abc.jks.jks"
var=`echo $strin | grep ".jks$"`
if [ "$var" = "$strin" ]; then
        echo "yes"
else
        echo "no"
fi
exit

Sponsored Links
    #6  
Old 08-18-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 12 October 2014, 10:11 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
How can I use parameter expansion? How can I get substrings? How can I get a file without its extension, or get just a file's extension?


Code:
# VAR="/usr/sample.abc.jks.jks"
# if [ ${VAR##*.} == "jks" ];then echo OK;else echo NOK;fi
OK


@ necroman08

Quote:
Code:
var=`echo $strin | grep ".jks$"`

Useless use of backticks, echo and grep !
Sponsored Links
    #7  
Old 08-18-2009
thanhdat's Avatar
thanhdat thanhdat is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 5 June 2014, 5:09 AM EDT
Location: Paris
Posts: 120
Thanks: 9
Thanked 2 Times in 2 Posts
an alternative solution:

Code:
myvar="/usr/sample.abc.jks.jks"

if [ `sed 's/.*\./\./g' <<< $myvar` == ".jks" ]; then
echo "good"
else
echo "bad"
fi

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
greping last occurrence of the searched string vidurmittal Shell Programming and Scripting 4 07-28-2009 07:06 AM
Displaying only First Occurrence of a String charudpss Shell Programming and Scripting 5 01-21-2009 03:55 AM
SED replace string by occurrence uttamhoode Shell Programming and Scripting 4 03-05-2008 04:04 AM
last occurrence of a string across multiple files porphyrin UNIX for Dummies Questions & Answers 2 12-24-2007 08:39 AM
Search and replace to first occurrence of string gilmord UNIX for Dummies Questions & Answers 7 05-03-2006 07:43 AM



All times are GMT -4. The time now is 06:51 PM.