Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nawk code - problem 1

Status
Not open for further replies.

RaviPydi

Programmer
Jul 23, 2004
4
US
Hi all,
I am trying to replace a string in a line using the "sub" function of "nawk" command.

I want to remove the "{${DB2_HOME}/lib:" from the line in a file called Env.sh and copy to a file called Env_new.sh

Here is my code:

echo " INFO: Modifying the Env.sh file"
nawk '
{FS="="}
$0 ~ /^SHLIB_PATH=/ && $0 ~ /DB2_HOME/ {
print "#"$0"";
sub("{${DB2_HOME}/lib:", ""); print ;
next }
{print;next}
' Env.sh > Env_new.sh

The data in the file is
SHLIB_PATH=${JRE_BIN}:${JRE_LIB}:${CROSSWORLDS}/lib:${CROSSWORLDS}/bin:${MQ_LIB}:${DB2_HOME}/lib:${WREQUIRED}:$SHLIB_PATH

The code works fine if I do not include "$" in the function sub("{${DB2_HOME}/lib:", "");. But I need to remove that "$" too from the String. I did try using the \ (escape) but that did not work either. My Field Separator (FS) is already set to "=" which I am using at some other part of the code.

Any suggestions on how I can overcome with this problem.

Thanks,
Ravi Pydi
 
Just a workaround:
sed 's!${DB2_HOME}/lib:!!' Env.sh | nawk -F'=' 'Your awk script' > Env_new.sh

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
RaviPydi
did you still realized all {} here
are not just stupid, but confusing
and useless ?
SHLIB_PATH=${JRE_BIN}:${JRE_LIB}:${CROSSWORLDS}/lib:
${CROSSWORLDS}/bin:${MQ_LIB}:${DB2_HOME}/lib:${WREQUIRED}:
$SHLIB_PATH

do you know when '{}' are needed ?

phv:
yes, but why use sed AND awk ?
sed could do the job... awk also.

4 both:
could you please randomly put a \n char
so i can read your post without use of horiz-scroll-bar ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top