Newbie Awk Program 2 
Author Message
 Newbie Awk Program 2

Hi,
        I am a newbie with awk.
I haven't learnt it formally. I have just read the manual & tried
to write a couple of programs. It seems to be working but I would
be grateful if somebody points out if I am doing things the accepted
way or is there any easier ways to do what I want.

This is a shell script which generates & calls an awk program.

I have config file which has the following format

PARAM1 VALUE1
PARAM2 VALUE2

PARAMETERBLOCK
BLOCKNAME       NAME1
PAR1    VAL1
PAR2    VAL2

PARAMETERBLOCK
BLOCKNAME       NAME2
PAR1    VAL1
PAR2    VAL2

i.e. there are some parameters which occur only once in a file
like PARAM1, PARAM2

i.e There are some which occur in blocks like PAR1, PAR2 etc.
However, they have a block name identified by NAME1, NAME2 etc.

The script

#!/bin/sh
# Parameters
# $1 = Config File Name
# $2 = Parameter
# $3 = Value
# $4 = Block Name [ can be null, in which case the parameter occurs
outside a block ]

if [ "x$4" = "x" ]
then

cat << E_O_F > a.awk
\$1 !~ /^$2$/ { print \$0 }
\$1 ~ /^$2$/ { print "$2 $3" }
E_O_F
else
cat << E_O_F > a.awk
\$1 ~ /^BLOCKNAME$/ { if (\$2 == "$4")
        fl=1
else
        fl=0

Quote:
}

\$1 ~ /^$2$/ { if (fl == 1) print "$2      $3"
        else print \$0 }
\$1 !~ /^$2$/ { print \$0 }
E_O_F
fi

awk -f a.awk $1 > t1
mv t1 $1

--
cheers,
Shiva
http://www.*-*-*.com/ ~shiva/
clc++ FAQ : http://www.*-*-*.com/ ~mpcline/c++-faq-lite/



Sun, 03 Mar 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Newbie Awk Program 1

2. AWK newbie is looking for a AWK help with his 1st program

3. An Awk Program to Create an Awk Program [Long]

4. Newbie question -- to make an awk program act on a collection of files

5. Newbie awk (sed??) question, regular expressions

6. another newbie problem - printing in awk

7. A Newbie AWK question

8. newbie needs help with multiple expressions in awk

9. AWK for DOS - NewBie Question

10. newbie awk question

11. awk newbie needs to parse xml file

12. Awk newbie seeking assistance

 

 
Powered by phpBB® Forum Software