Jump to content
 English      
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
     Forums advanced search
HP.com Home
IT Resource Center Forums > HP-UX > general

how to parse using shell quoting

» 

IT Resource Center

» Login
» Register
» My profile
» Search knowledge base
» Forums
» Patch database
» Download drivers, software and firmware
» Warranty check
» Support Case Manager
» Software Update Manager
» Training and Education
» More maintenance and support options
» Online help
» Site map

Member icons
 
 HP moderator  HP moderator
 Expert in this area  Expert in this area
Member status
ITRC Pro ITRC Pro
250 points
ITRC Graduate ITRC Graduate
500 points
ITRC Wizard ITRC Wizard
1000 points
ITRC Royalty ITRC Royalty
2500 points
ITRC Pharaoh ITRC Pharaoh
7500 points
Olympian Olympian
20000 points
1-Star Olympian 1-Star Olympian
40000 points
2-Star Olympian 2-Star Olympian
80000 points
»  How to earn points
»  Support forums FAQs
Question status
Magical answer Magical answer
Message with a response that solved the author's question
Favorites status
Add to my favorites Add to my favorites
Delete from my favorites Delete from my favorites
This thread has been closed Thread closed
 

Content starts here
   Create a new message    Receive e-mail notification if a new reply is posted  Reply to this message
Author Subject: how to parse using shell quoting      Add to my favorites
Oscar Sastre This member has accumulated 500 or more points
Jul 11, 2003 10:06:19 GMT   

Hi

Is there any way to take advantage of shell features as quoting and parameter substitution in my own scripts ?

For instance, supose you have to write down a script in order to parse a configuration file (text, not binary). The parameters assigned in it may contain blank chars, so quoting is in mind... You know that Korn shell and Posix shell can parse source lines in order to substitute values and alias before launching commands.

Is it possible to get advantage of these shell features or I have to write down my own parser ?

Thanx in advance
Note: If you are the author of this question and wish to assign points to any of the answers, please login first.For more information on assigning points ,click here


Sort Answers By: Date or Points
Ollie R Expert in this area This member has accumulated 500 or more points
Jul 11, 2003 10:49:51 GMT  0 pts

Do you have an example of what you're trying to achieve here?
James R. Ferguson This member has accumulated 80000 or more points
Jul 11, 2003 10:58:10 GMT  3 pts

Hi:

One way is to leverage the shell's 'read' command with the Inter-Field Separator (IFS). A common use would be reading a colon-delimited file lie '/etc/passwd':

#!/usr/bin/sh
echo "...using COLON as IFS..."
IFS=\:
while read A B C X
do
echo "A=${A} B=${B} C=${C} LEFT=${X}"
done < /etc/passwd
IFS=${OLDIFS}
exit 0

Regards!

...JRF...
James R. Ferguson This member has accumulated 80000 or more points
Jul 11, 2003 11:22:53 GMT  2 pts

HI (again):

Here's another crude example:

#!/usr/bin/sh
IFS=\,
while read A B C X
do
FIRST=`echo ${A}|sed -e 's/"//g' | cut -d" " -f1`
LAST=`echo ${A}|sed -e 's/"//g' | cut -d" " -f2`
SEX=`echo ${C}|sed -e 's/"//g'`
echo "FIRST=${FIRST} LAST=${LAST} SEX=${SEX}"
done < /tmp/parse.stuff
IFS=${OLDIFS}
exit 0

Run above using an 'infile' of comma-delimited, quoted fields like:

"James Ferguson","husband","male"
"Shirlee Ferguson","wife","female"
"Jessica Ferguson","daughter","female"
"Jamie Ferguson","daughter","female"

Regards!

...JRF...
James R. Ferguson This member has accumulated 80000 or more points
Jul 11, 2003 11:51:42 GMT  3 pts

Hi (again):

OK, after a bit of coffee, here's another shell feature that can be leveraged -- 'set' and array assignment. Consider this crude script:

#!/usr/bin/sh
IFS=\,
while read LINE
do
set -A ARY ${LINE}
FIRST=`echo ${ARY&#91;0&#93;}|sed -e 's/"//g' | cut -d" " -f1`
LAST=`echo ${ARY&#91;1&#93;}|sed -e 's/"//g' | cut -d" " -f2`
SEX=`echo ${ARY&#91;2&#93;}|sed -e 's/"//g'`
echo "FIRST=${FIRST} LAST=${LAST} SEX=${SEX}"
done < infile
IFS=${OLDIFS}
exit 0

Use the same input file ('infile') I used with my previous post, above, with the format:

"Firstname Lastname","Title","Sex"

...as the fields to dissect.

Regards!

...JRF...
Oscar Sastre This member has accumulated 500 or more points
Jul 14, 2003 08:26:06 GMT    N/A: Question Author

Supose you have a configuration file as:

FULL_NAME="Brew, John Peter" # full name
INITIALS=B_JP # initials
MAIN_FILE="/direct/people/${INITIALS}.data"
EXT='#1234'

in this example you have characters as '#' used to insert comments as lines 2 and 3 (but see the last line where # is not inerting a comment).

You also have to parse blank chars as a part of an assignment (first line).

You also see one parameter INITIALS used to compound the value of other parameter (MAIN_FILE).
Steve Steel This member has accumulated 7500 or more points
Jul 14, 2003 09:14:41 GMT  4 pts

Hi

Use your config file as input

#Give file name as parameter and ensure it can be executed.
file=$1
. $file
echo User $FULL_NAME with Initials $INITIALS on extension $EXT uses $MAIN_FILE

Then it is parsed

Steve Steel
Gregory Fruth This member has accumulated 1000 or more points
Jul 14, 2003 19:58:48 GMT  2 pts

Steve's suggestion to use the "." operator is right on
the mark. However, note that the "." operator uses
PATH, so you may want to temporarily add the
current directory to the head of PATH or require that
the name of the config file is always specified exactly.
Otherwise, if the name of the config file clashes with
a program somewhere in PATH you'll get unexpected
results.

For example, suppose the config file is called "test".
Then if your script does:

cfg="test"
echo FOO
. $cfg
echo BAR

The output will only be FOO, because the "." searches
through the dirs in PATH and will likely find "/usr/bin/test"
before it finds "./test", and "/usr/bin/test" does an "exit"
which will terminate the program. To fix this, try something
like:

PATH=.:$PATH
cfg="test"
echo FOO
. $cfg
echo BAR

or:

cfg="./test"
echo FOO
. $cfg
echo BAR
 
Create a new message    Receive e-mail notification if a new reply is posted   Reply to this message
 
 
Printable version
Privacy statement Using this site means you accept its terms
© 2010 Hewlett-Packard Development Company, L.P.