Google Answers Logo
View Question
 
Q: Need help on a specific regular expression. ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Need help on a specific regular expression.
Category: Computers > Programming
Asked by: cheekoo1-ga
List Price: $10.00
Posted: 05 Jan 2004 13:25 PST
Expires: 04 Feb 2004 13:25 PST
Question ID: 293407
I need help on phrasing the correct regular expression or shell script
for the following :
url=/xxx/yyy/zzz/a.jsp /xxx/yyy/zzz/b.jsp /xxx/yyy/zzz/c.jsp /xxx/yyy/aaa/d.jsp
Now, when i try to output the value of variable "url" into a file it
goes as one line like the following :
==========================================================================
/xxx/yyy/zzz/a.jsp /xxx/yyy/zzz/b.jsp /xxx/yyy/zzz/c.jsp /xxx/yyy/aaa/d.jsp  
===========================================================================

I need it to go like 
/xxx/yyy/zzz/a.jsp
/xxx/yyy/zzz/b.jsp
/xxx/yyy/zzz/c.jsp
/xxx/yyy/aaa/d.jsp
basically one JSP on each line.

I can use Perl, sh, sed awk for this work.

Request for Question Clarification by mathtalk-ga on 05 Jan 2004 13:45 PST
Hi, cheekoo1-ga:

If the line breaks are actually stored in shell variable url, then you
may not be seeing them because of the Unix shell removing whitespace
(spaces, tabs, newlines) from the string before doing the substitution
(leaving only a single space as a delimiter).  This is generally what
one wants in constructing command line arguments.

To preserve the whitespace (and in particular any embedded line
breaks) enclose the value of the script variable in double quotes,
like this:

"$url"

when you use it.  Give it a try, e.g. with an echo command, and see if
that makes the difference you want.

regards, mathtalk-ga

Clarification of Question by cheekoo1-ga on 05 Jan 2004 14:21 PST
Hi Mathtalk:ga, 
      Thanks for your suggestion, but that did not seem to fix it.Let
me give u some more context. I have a makefile like the following :

===============================================================================
JSP_LONGDIR=/opt/www/options
# Only include files here that are root JSPs (compilable ones)
FILES+=$(JSP_LONGDIR)/profileList.jsp
FILES+=$(JSP_LONGDIR)/profileListSubmit.jsp
FILES+=$(JSP_LONGDIR)/editProfileName.jsp

all: local 
local: 
	@$(SH) /opt/bin/JspLister.sh $(FILES) || exit 1;
=============================================================================
The JspCompiler.sh file now contains somethihng like the following :

urls="$*"
urls=`echo $urls | perl -pe 's{[^ ]+/www/}{/}g'`

if [ ! -z "$ANT_BUILD" ]
then
	echo -e "ANT_BUILD variable is set ."
	{ 
		echo "$urls"
	} >> "op.test"       # Redirects output of everything in block to file.
        exit
fi
=================================================
if I look inside the "op.test" output file , the listing that I get
contains one seperate line for each makefile that is used.But that one
line may or may not contain multiple JSP listings.
That is the problem that I am trying to solve.

eg : The following needs to be changed FROM
/index.jsp
/ac/index.jsp
/ANZ/admin/managers.jsp /ANZ/admin/messaging.jsp
/ANZ/admin/purview.jsp /ANZ/admin/purviewmessaging.jsp

to 

/index.jsp
/ac/index.jsp
/ANZ/admin/managers.jsp
/ANZ/admin/messaging.jsp 
/ANZ/admin/purview.jsp 
/ANZ/admin/purviewmessaging.jsp

Clarification of Question by cheekoo1-ga on 05 Jan 2004 14:23 PST
Sorry for the typo ...
please read 
The JspCompiler.sh file now contains somethihng like the following : as
The JspLister.sh file now contains somethihng like the following :

Request for Question Clarification by maniac-ga on 05 Jan 2004 18:29 PST
Hello Cheekoo1,

I am not sure which shell you are using, I'll assume sh (and not csh).

Have you tried something like...

#!/bin/sh                                                                       
if [ ! -z "$ANT_BUILD" ]
then
        echo -e "ANT_BUILD variable is set ."
else
    for N in $* ;  do
        echo "$N" >> "op.test"
    done
fi

which will append the arguments, one per line, to the op.test file. If
this meets your needs, I'll post this (and an explanation) as a
complete answer.

  --Maniac

Clarification of Question by cheekoo1-ga on 06 Jan 2004 03:49 PST
Well thanks !!! 

I think that I will definitely grant that answer to you, if you just
describe the solution in detail.The how and why of the answer ... Also
caveats /Exceptions if any ...

I am not sure if what I am asking now warrants a another question.I
want to know if there is a way to extrapolate
/axn/xyz/*.jsp to let us say 5 lines like 

/axn/xyz/1.jsp 
/axn/xyz/2.jsp 
/axn/xyz/3.jsp 
/axn/xyz/4.jsp 
/axn/xyz/5.jsp
Answer  
Subject: Re: Need help on a specific regular expression.
Answered By: maniac-ga on 06 Jan 2004 05:11 PST
Rated:5 out of 5 stars
 
Hello Cheekoo1,

For your specific problem (writing shell arguments into a file, one
per line), the solution can be something like...

#!/bin/sh                                                                       
if [ ! -z "$ANT_BUILD" ]
then
        echo -e "ANT_BUILD variable is set ."
else
    for N in $* ;  do
        echo "$N" >> "op.test"
    done
fi

Which is explained as follows:

#!/bin/sh - starts the file with a "magic number" indicating this is a
shell script and you should use the version located at /bin/sh
(instead of /bin/csh or another).

the if clause was taken from your example

the for clause works like this:
  $* is a special parameter and expands to the positional parameters
starting at one (skipping the name of the script at $0). Note - you
might want to use "$@" instead due to how spaces in arguments (file
names) are handled.
  N is a variable will be assigned to each value of $* for each pass
through the loop
and the loop echos the value of $N and appends it to each file.

This information was found by using
  man sh
from a command line interface to bring up the shell man page. For
Linux, you can also view
  http://www.die.net/doc/linux/man/man1/sh.1.html
for a recent version of this same information. If you don't use Linux,
try a search such as
  sh man page Solaris
to find the Sun Solaris page (or replace Solaris by your version of Unix).

To answer your follow up question, if you use...
  /opt/bin/JspLister.sh /anx/xyz/*.jsp
the shell will expand
  /anx/xyz/*.jsp
into the five arguments
  /axn/xyz/1.jsp 
  /axn/xyz/2.jsp 
  /axn/xyz/3.jsp 
  /axn/xyz/4.jsp 
  /axn/xyz/5.jsp
and the shell script should add those five lines to the output file (op.test).

You may also want to review some introductory material by searching with
  shell programming tutorial
  shell programming introduction
and similar phrases.

Don't hesitate to ask for clarification if this answer does not meet your needs.
  --Maniac
cheekoo1-ga rated this answer:5 out of 5 stars
Good effort MANIAC !!! Thanks for all the info, I will give the answer
to you right now, that way we finish off the transaction ASAP.However
the extrapolation from /axn/xyz/*.jsp to the individual JSPs did not
work for me.

I will add the script code if you can suggest something about it.Also
I am greatful for your referrences about shell programming in
general.The code of my script is as follows :

#!/usr/local/bin/bash

. ${BELLS}/bin/runtime_env.sh

cd `convert_dos_pathname ${RESIN_HOME}`

#  This can be removed when all the makefiles in www are changed to 
#  specify the path relative to www instead of the full path
urls="$*"
urls=`echo $urls | perl -pe 's{[^ ]+/www/}{/}g'`

if [ ! -z "$ANT_BUILD" ]
then
	echo -e "ANT_BUILD variable is set .  Hence changing implementation
of startResinJspCompiler.sh."
	for N in $urls ;  do
	    echo "$N" >> "jspslist.txt"
	done
	exit
fi

JAVA_HOME=$BLS_RESIN_SERVER_JDK
echo ${BLS_CPATH}
${EXEC} ${BLS_RESIN_SERVER_JDK}/bin/java \
    ${JAVA_WRAPPER_OPTS} \
    -DBELLS.programName="[ResinJspCompiler]" \
    ${USER_JAVA_PROPS} \
    -ms32m\
    -mx32m \
    -classpath "${BLS_CLPATH}" \
    -DBELLS.resin.http.port="${BLS_RESIN_HTTP_PORT}" \
    BELLS.util.BLSStartup BELLS.util.BLSResinJspCompiler $urls

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy