- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- accessing ksh variables
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2007 06:22 PM
02-25-2007 06:22 PM
accessing ksh variables
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2007 08:34 PM
02-25-2007 08:34 PM
Re: accessing ksh variables
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2007 08:34 PM
02-25-2007 08:34 PM
Re: accessing ksh variables
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2007 08:38 PM
02-25-2007 08:38 PM
Re: accessing ksh variables
The itrc webserver wasn't as responsive.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2007 11:16 PM
02-25-2007 11:16 PM
Re: accessing ksh variables
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
before
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
# 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