Newbie Awk Program 1 
Author Message
 Newbie Awk Program 1

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 this format
param1  param2  param3 param4
This shell script modifies a record in the config file
or adds a record or deletes a record

#!/bin/sh
# Parameters
# $1 = what operation to perform
#      0 -> delete a record for param1 = $3
#      1 -> add a record
#      2 -> modify a record with param1 = $3
# $2 = config file
# $3 = param1
# $4 = param2
# $5 = param3
# $6 = param4

case $1 in
0)
cat << E_O_F > a.awk
\$1 !~ /^$3$/ { print \$0 }
E_O_F
awk -f a.awk $2 > t1
mv t1 $2
;;
1)
echo $3 $4 $5 $6 >> $2
;;
2)
cat << E_O_F > a.awk
\$1 ~ /^$3$/ { print "$3 $4 $5 $6" }
\$1 !~ /^$3$/ { print \$0 }
E_O_F
awk -f a.awk $2 > t1
mv t1 $2
;;
esac

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



Sun, 03 Mar 2002 03:00:00 GMT  
 Newbie Awk Program 1

%       I am a newbie with awk.
% I haven't learnt it formally. I have just read the manual & tried

Of course, most people spend years learning this stuff before they post
to the group, but I suppose we can make an exception in your case...

% #!/bin/sh

People commonly do this. I prefer to start awk scripts with #!/usr/awk/awk -f

% case $1 in
% 0)
% cat << E_O_F > a.awk
% \$1 !~ /^$3$/ { print \$0 }
% E_O_F

But this is an interesting twist. This is what we like to call in the awk
academies a `one-liner', meaning it all fits on one line, comme ?a:
 awk "\$1 != /^$3\$/" $2 > t1

Even if you couldn't do it in a one-liner, you might as well use the HERE
document as an argument to awk:
 awk -f - $2 > t1 <<HERE
...
HERE

Apart from that, it's a pretty standard shell-script-which-uses-awk.

--

Patrick TJ McPhee
East York  Canada



Mon, 04 Mar 2002 03:00:00 GMT  
 Newbie Awk Program 1

Quote:

>Even if you couldn't do it in a one-liner, you might as well use the HERE
>document as an argument to awk:
> awk -f - $2 > t1 <<HERE
>...
>HERE

Very nice.  Didn't know -f can use "-" to specify <stdin>.  I will
definately use.  However, doesn't one need to quote "HERE" to
tell the shell to lay off substituting for awk $ variable names?
And <<HERE can go anywhere on the command line, so for
better readability, would you want either of these two forms?:

      awk -f - <<\HERE $2 >t1
      awk -f - 'HERE' $2 >t1



Mon, 04 Mar 2002 03:00:00 GMT  
 Newbie Awk Program 1

% Very nice.  Didn't know -f can use "-" to specify <stdin>.  I will
% definately use.  However, doesn't one need to quote "HERE" to
% tell the shell to lay off substituting for awk $ variable names?

I guess it depends on whether you want the shell to do that or not.
(OK, it's a good point.)
--

Patrick TJ McPhee
East York  Canada



Tue, 05 Mar 2002 03:00:00 GMT  
 Newbie Awk Program 1


Quote:


> >Even if you couldn't do it in a one-liner, you might as well use the
HERE
> >document as an argument to awk:
> > awk -f - $2 > t1 <<HERE
> >...
> >HERE

> Very nice.  Didn't know -f can use "-" to specify <stdin>.  I will
> definately use.  However, doesn't one need to quote "HERE" to
> tell the shell to lay off substituting for awk $ variable names?
> And <<HERE can go anywhere on the command line, so for
> better readability, would you want either of these two forms?:

>       awk -f - <<\HERE $2 >t1
>       awk -f - 'HERE' $2 >t1

I probably miss the whole point... Why not just give the program as an
argument to awk?

awk ' \
...
...
' $2 > t1

Depending on if you want the shell to expand variables or not you can
choose either double or single quotes.

/Peter
--
-= Spam safe(?) e-mail address: pez68 at netscape.net =-

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Tue, 05 Mar 2002 03:00:00 GMT  
 Newbie Awk Program 1

Quote:

>      awk -f - <<\HERE $2 >t1
>      awk -f - 'HERE' $2 >t1

OOPs.  In my e{*filter*}ment over my new awareness of "-f -" (thanks to
comp.lang.awk) I made boo boo.  Don't want to let it go too long
before correcting.  The second form above is patently wrong.
Sorry.  Should be either:

      awk -f - <<\HERE  $2 >t1
      awk -f - <<'HERE'  $2 >t1



Tue, 05 Mar 2002 03:00:00 GMT  
 Newbie Awk Program 1

% I probably miss the whole point... Why not just give the program as an
% argument to awk?

For a long script, it's easier to use a HERE document. You don't have to
worry about shell line-length limits, you don't have trouble searching for
the end of the script, you aren't faced with the temptation to use stupid
quoting tricks to import shell variables, and you don't have to worry
about how to include quote characters in your script. On the other hand,
there are no benefits to including a long script as an argument to awk.

--

Patrick TJ McPhee
East York  Canada



Thu, 07 Mar 2002 03:00:00 GMT  
 Newbie Awk Program 1


Quote:


> % I probably miss the whole point... Why not just give the program as
an
> % argument to awk?

> For a long script, it's easier to use a HERE document. You don't have
to
> worry about shell line-length limits, you don't have trouble
searching for
> the end of the script, you aren't faced with the temptation to use
stupid
> quoting tricks to import shell variables, and you don't have to worry
> about how to include quote characters in your script. On the other
hand,
> there are no benefits to including a long script as an argument to

awk.

Well, one obvious benefit is that you can do stupid quoting tricks to
access shell variables. =)

/Peter
--
-= Spam safe(?) e-mail address: pez68 at netscape.net =-

Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.



Fri, 08 Mar 2002 03:00:00 GMT  
 Newbie Awk Program 1

Quote:

>Well, one obvious benefit is that you can do stupid quoting tricks to
>access shell variables. =)

True.  Most of the time it is fielder's choice.  I propose going to
HERE document for two reasons for sure:
(1) more than about 1/2 page of code, to ensure minor maintenance
by someone else doesn't expand the program past the string limit.

(2) when generating another computer language (like SQL) that
requires single quotes.  Worse yet, many such languages being
generated require their quotes to be escaped or doubled.  Keeping
track of how to write the awk quote to generate the language
quote to produce the data quote and get the whole thing through
a shell is too {*filter*} the head.

BTW, I'm relatively new here.  Y'all use '\'' to get the quote
to awk through a shell?  My fingers do that without any conscious
thought.  A maintenance programmer needs to recognize that
construct.  However, too many of them is just being mean to
thy fellow awk'er.

Is the consensus that, either way, the awk program(s) should
be in the containing shell scrip?.  True?  No subsidiary .awk
files, right?



Sat, 09 Mar 2002 03:00:00 GMT  
 Newbie Awk Program 1


Quote:

> >Well, one obvious benefit is that you can do stupid quoting tricks to
> >access shell variables. =)

> True.  Most of the time it is fielder's choice.  I propose going to
> HERE document for two reasons for sure:
> (1) more than about 1/2 page of code, to ensure minor maintenance
> by someone else doesn't expand the program past the string limit.

True, but I would say more than 3 pages or so..

Quote:
> (2) when generating another computer language (like SQL) that
> requires single quotes.  Worse yet, many such languages being
> generated require their quotes to be escaped or doubled.  Keeping
> track of how to write the awk quote to generate the language
> quote to produce the data quote and get the whole thing through
> a shell is too {*filter*} the head.

Oh, yes. I could not agree more. But I would definately put such an awk-
script in a file by itself and invoke it with -f.

Quote:
> BTW, I'm relatively new here.  Y'all use '\'' to get the quote
> to awk through a shell?  My fingers do that without any conscious
> thought.  A maintenance programmer needs to recognize that
> construct.  However, too many of them is just being mean to
> thy fellow awk'er.

I didn't use that '\''. But if you are to maintain scipts I would say
you must recognize such stuff.

Quote:
> Is the consensus that, either way, the awk program(s) should
> be in the containing shell scrip?.  True?  No subsidiary .awk
> files, right?

I think it's quite the contrary. I have pointed out that I think it has
it's advantags at times. But I have gotten almost ridiculed then. =) Of
course .awk files has their place. That maintenance scenario is one
reason. If you are set to maintain awk scripts, then you don't have to
also be fluent in shell scripting.

/Peter
--
-= Spam safe(?) e-mail address: pez68 at netscape.net =-

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Sat, 09 Mar 2002 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Newbie Awk Program 2

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