niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » shell
Interpret variables in strings in ksh
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
Author Message
William
*nix forums Guru Wannabe


Joined: 04 Apr 2005
Posts: 196

PostPosted: Wed Jul 19, 2006 8:04 pm    Post subject: Re: Interpret variables in strings in ksh Reply with quote

<greenhakan@gmail.com> wrote in message
news:1153317834.681930.8790@i42g2000cwa.googlegroups.com...
Quote:
Hi all,
in a ksh script I want to be able to interpret strings containing
some kind of variables.
The syntax of such a 'variable' is not so important, let's assume
that a variable looks like this: ?var?
So if the string looks like
blabla/something/?var1?/somethingelse/?var2?/etc
I want to be able to extract the strings 'var1' and 'var2'.
The string can contain zero or more of these 'variables', and
there can be many variables in sequence: bla?var1??var2?bla

This doesn't sound like interpreting, just string extraction.
Here's a hack that seems to work (probably has some issues,
I had to cheat and append ?? to the input string to get it to
drop the trailing /etc, for example). Just a starting point
for you:

#!/bin/sh
testStr='blabla/something/?var1?/somethingelse/?var2?/etc'

set -- `echo "$testStr??" | sed -e 's|[^?]*?\([^?]*\)?|\1 |g'`
echo "String: $testStr"
echo "String contains $# variables:"
while [ $# -gt 0 ] ; do
echo " $1"
shift
done

-Wm
Back to top
John DuBois
*nix forums addict


Joined: 30 Mar 2005
Posts: 61

PostPosted: Wed Jul 19, 2006 3:19 pm    Post subject: Re: Interpret variables in strings in ksh Reply with quote

In article <1153317834.681930.8790@i42g2000cwa.googlegroups.com>,
<greenhakan@gmail.com> wrote:
Quote:
Hi all,
in a ksh script I want to be able to interpret strings containing
some kind of variables.
The syntax of such a 'variable' is not so important, let's assume that
a variable looks like this: ?var?
So if the string looks like
blabla/something/?var1?/somethingelse/?var2?/etc
I want to be able to extract the strings 'var1' and 'var2'.
The string can contain zero or more of these 'variables', and there can
be many variables in sequence: bla?var1??var2?bla

Any ideas?

j=bla?var1??var2?bla

oIFS=$IFS
IFS=\?
set -- $j
IFS=$oIFS
while (( $# > 1 )); do
print -- "$2"
shift 2
done

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Back to top
greenhakan@gmail.com
*nix forums beginner


Joined: 19 Jul 2006
Posts: 1

PostPosted: Wed Jul 19, 2006 2:03 pm    Post subject: Interpret variables in strings in ksh Reply with quote

Hi all,
in a ksh script I want to be able to interpret strings containing
some kind of variables.
The syntax of such a 'variable' is not so important, let's assume that
a variable looks like this: ?var?
So if the string looks like
blabla/something/?var1?/somethingelse/?var2?/etc
I want to be able to extract the strings 'var1' and 'var2'.
The string can contain zero or more of these 'variables', and there can
be many variables in sequence: bla?var1??var2?bla

Any ideas?
Regards, Håkan
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [3 Posts] View previous topic :: View next topic
The time now is Thu Dec 04, 2008 1:36 am | All times are GMT
navigation Forum index » Programming » shell
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Quoting, variables, and xterm graftonfot@yahoo.com shell 2 Thu Jul 20, 2006 2:26 pm
No new posts Question on comparing to variables containing integers huub Perl 14 Thu Jul 20, 2006 11:02 am
No new posts Help me Interpret these Logs DB_BOY AIX 1 Thu Jul 20, 2006 8:45 am
No new posts Retrieve ext. variables in python program alfa1234 python 7 Wed Jul 19, 2006 7:59 am
No new posts How to add variables to strings? yusufm Perl 6 Tue Jul 18, 2006 7:02 pm

Ipod shuffle | Tutorial eBook PDF Download | Buy Anything On eBay | Loan | Payday Loan
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1370s ][ Queries: 20 (0.0649s) ][ GZIP on - Debug on ]