1825771 Members
2306 Online
109687 Solutions
New Discussion

accessing ksh variables

 
Trupti Wagh
New Member

accessing ksh variables

hi everybody!
i am running this ksh script for replacing a set of strings by another set of new ones. i am getting both these from a file.
also, the strings that i want to replace, are sub-strings(can occur more than once in each chunk) in a big chunk of data that i have bulk-copied(bcp utility) from a database. i can do it successfully, but it so happens that some of the set of strings that need to be replaced are mutually sub-strings of eachother. like for example, the file containing the sets of strings includes the following::

abc abc:x
abcd abcl.x
...etc.

now while replacement with the first set of strings the instances of the second string get altered...
abc:xd

i am trying to use gawk FIELDWIDTHS for this with the following code snippet:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cp ${ORIGFILE} ${TMPFILE}

sed "s/\*/\\\*/g" ${SYMLISTFILE} > ${NEWSYMLIST}
while read OLDSYM NEWSYM FLDLEN
do
gawk 'BEGIN{ FIELDWIDTHS = ${FLDLEN} } END{ s/${OLDSYM}/${NEWSYM}/g }'
# sed "s/${OLDSYM}/${NEWSYM}/g" ${TMPFILE} > ${INFILE}
cp ${INFILE} ${TMPFILE}
done < ${NEWSYMLIST}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
while running the script now i get the following error for the ${FLDLEN}::
gawk: cmd. line:1: BEGIN{ FIELDWIDTHS = ${FLDLEN} } END{ s/${OLDSYM}/${NEWSYM}/g }
gawk: cmd. line:1: ^ parse error

please can anyone help me writing a viable code that can access the FLDLEN from the ksh script to the gawk script?
is gawk freely available to install... and is its installation the solution?

Thanking you in advance,
Tru.
4 REPLIES 4
Ralph Grothe
Honored Contributor

Re: accessing ksh variables

I apologize, but I haven't carefully read your request.
But if it's only passing shell variables to awk this is quite simply done by appending a list of parameters to your awk invocation like this:

e.g.

SH_VAR1=Hello
SH_VAR2=World

awk '{some valid awk here}' /some/file/to/parse_over var1=SH_VAR1 var2=SH_VAR2

In your awk code you can refer to var1, var2, ...

More modern awk versions (like gawk, or HP's awk) also offer the -v option to pass shell variables (see manpage)

HTH

Ralph
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: accessing ksh variables

I apologize, but I haven't carefully read your request.
But if it's only passing shell variables to awk this is quite simply done by appending a list of parameters to your awk invocation like this:

e.g.

SH_VAR1=Hello
SH_VAR2=World

awk '{some valid awk here}' /some/file/to/parse_over var1=$SH_VAR1 var2=$SH_VAR2

In your awk code you can refer to var1, var2, ...

More modern awk versions (like gawk, or HP's awk) also offer the -v option to pass shell variables (see manpage)

HTH

Ralph
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: accessing ksh variables

Sorry, for that twin posting again.
The itrc webserver wasn't as responsive.
Madness, thy name is system administration
Peter Nikitka
Honored Contributor

Re: accessing ksh variables

Hi,

first: you'll have to sort your substition pattern in reverse order, so no multiple operations are done for one pattern.

Second: your gawk sysntax is broken:
- ${var} is NOT a valid awk syntax
- single quoting of for commands will prevent shell variables to be expanded.

My solution creates an sed-script for your changes. Since there may be only 99 lines in a sed script, thre may be the need to split one langer sed commandfile into pieces ('man split').

I assume, the format of NEWSYMLIST is
beforeafter
and there is NO SPACE in both pattern 'before' and 'after'. Note that a 's' command of sed will take the first char following as the delimiter, which will be a SPACE in our case here.

sort -r -k1 $NEWSYMLIST |
sed -e 's/^/ /' -e 's/$/ d/' >/tmp/sed.cmd

if [ $(wc -l
then
# Only move if it was successfull
if sed -f /tmp/sed.cmd $ORIGFILE >$TMFILE
then
cp $ORIGFILE $ORIGFILE.orig
cp $TMFILE $ORIGFILE
fi
else # you have to split
fi

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"