LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-16-2007, 12:21 PM   #1
ocicat
Member
 
Registered: May 2007
Posts: 207

Rep: Reputation: 46
performing regex on shell script variable?


I'm simply not familiar with the intricacies of C shell scripting.

How do I change the contents of a variable through a regular expression? Assuming I want to simplify an absolute pathname by using tilde notation, how would this be done within a C shell script?

In Perl, this would be done similarly to:

Code:
my $s = `pwd`;
$s =~ s/^\Q$ENV{HOME}/~/;
It appears that tools like grep or sed all work on files, so I'm confused given that the value I want to translate is already contained in a variable.

Thanks for any insight which can be shared.
 
Old 07-16-2007, 12:58 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
They quite happily work on anything you throw at them.

Code:
tmp=`echo $VARIABLE|sed 's/do_your/magic/'`
Use grep analogously :}


Cheers,
Tink
 
Old 07-17-2007, 01:26 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

The form of the assignment statement will vary from Bourne shell relatives (e.g. bash) to cshell (e.g. tcsh), but as Tinkster has written the key concept is to use echo. For example:
Code:
#!/bin/tcsh

# @(#) s1       Demonstrate set, setenv in tcsh.

echo
echo "$version" | sed 's/options.*//'
echo " Using shell $SHELL"

set script = "hello"

set SCRIPT="world"

setenv VAR " This will be an environment variable."

echo
echo " script is ${script}, SCRIPT is ${SCRIPT}"
echo " ( evaluating $script is same as ${script} )"
echo " The environment variable is :${VAR}:"
env | grep VAR

echo
echo " Argument 0 is $0"
set program = `basename $0`
echo " The result of basename on argument 0 is $program"
echo " All arguments from argv are $argv"

exit 0
producing:
Code:
% ./s1 one two three

tcsh 6.13.00 (Astron) 2004-05-19 (i386-intel-linux)
 Using shell /bin/tcsh

 script is hello, SCRIPT is world
 ( evaluating hello is same as hello )
 The environment variable is : This will be an environment variable.:
VAR= This will be an environment variable.

 Argument 0 is ./s1
 The result of basename on argument 0 is s1
 All arguments from argv are one two three
See man tcsh for details ... cheers, makyo
 
Old 07-17-2007, 02:10 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

As a follow-up, the later Bourne family shells can do some processing directly on variables:
Quote:
${parameter/pattern/string}
${parameter//pattern/string}
The pattern is expanded to produce a pattern just as in pathname
expansion. Parameter is expanded and the longest match of pat-
tern against its value is replaced with string. In the first
form, only the first match is replaced. The second form causes
all matches of pattern to be replaced with string. If pattern
begins with #, it must match at the beginning of the expanded
value of parameter. If pattern begins with %, it must match at
the end of the expanded value of parameter. If string is null,
matches of pattern are deleted and the / following pattern may
be omitted. If parameter is @ or *, the substitution operation
is applied to each positional parameter in turn, and the expan-
sion is the resultant list. If parameter is an array variable
subscripted with @ or *, the substitution operation is applied
to each member of the array in turn, and the expansion is the
resultant list.

-- man bash on patterns
For example:
Code:
#!/bin/sh

# @(#) s1       Demonstrate patterns and variables.

set -o nounset
echo
echo "GNU bash $BASH_VERSION" >&2
echo

t1="hello bad world"

echo " The value of t1 is :$t1:"
echo " Result of t1 processed :${t1/?bad?/ GOOD }:"

exit 0
producing:
Code:
% ./s1

GNU bash 2.05b.0(1)-release

 The value of t1 is :hello bad world:
 Result of t1 processed :hello GOOD world:
This is but one of the many reasons that most people prefer to write shell scripts in the Bourne shell family ... cheers, makyo
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script Random Variable Daniel Programming 14 12-03-2007 05:00 AM
shell script: redirect to variable va1damar Programming 1 02-05-2007 04:11 PM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 04:56 PM
expanding variable in shell script dipenchaudhary Programming 8 02-08-2006 05:05 PM
Help with shell script, can't show variable stormrider_may Programming 5 01-27-2006 05:22 PM


All times are GMT -5. The time now is 10:10 AM.

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