For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2004 > Reading in files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Reading in files
Robert Peirce

2004-05-12, 10:30 pm

I have a library of functions I use with various awk scripts. Generally
I just read them into the script. However, it is also possible to load
them with -f if the program script is also loaded that way. For
example, if the program is tst, the script is tst.awk and the function
library is fun.awk, then putting the following in tst will work.

awk -f fun.awk -f tst.awk

What I would like to know is if anybody knows a way to combine these two
methods so that you can read the functions in from a file but have the
script entered as in-line code. For example:

awk -f fun.awk '{
in-line-code
}'

This does not work, but you get the idea.

--
Robert B. Peirce, Venetia, PA 724-941-6883
bob AT peirce-family.com [Mac]
rbp AT cooksonpeirce.com [Office]

Ed Morton

2004-05-13, 8:30 am



Robert Peirce wrote:
> I have a library of functions I use with various awk scripts. Generally
> I just read them into the script. However, it is also possible to load
> them with -f if the program script is also loaded that way. For
> example, if the program is tst, the script is tst.awk and the function
> library is fun.awk, then putting the following in tst will work.
>
> awk -f fun.awk -f tst.awk
>
> What I would like to know is if anybody knows a way to combine these two
> methods so that you can read the functions in from a file but have the
> script entered as in-line code. For example:
>
> awk -f fun.awk '{
> in-line-code
> }'
>
> This does not work, but you get the idea.
>


I've never tried it, but for most UNIX commands that take a file name, a
single minus sign represents stdin so something like one of these MIGHT
work:

awk -f fun.awk -f -
'{
in-line-code
}'

awk -f fun.awk -f - <<!
'{
in-line-code
}'
!

cat <<! | awk -f fun.awk -f -
'{
in-line-code
}'
!

Regards,

Ed.

Manuel Collado

2004-05-13, 10:30 am

This question has also been asked in the UWIN-users mailing list. I'm
reposting my answer here.

Robert Peirce wrote:
> I have a library of functions I use with various awk scripts. Generally
> I just read them into the script. However, it is also possible to load
> them with -f if the program script is also loaded that way. For
> example, if the program is tst, the script is tst.awk and the function
> library is fun.awk, then putting the following in tst will work.
>
> awk -f fun.awk -f tst.awk
>
> What I would like to know is if anybody knows a way to combine these two
> methods so that you can read the functions in from a file but have the
> script entered as in-line code. For example:
>
> awk -f fun.awk '{
> in-line-code
> }'
>
> This does not work, but you get the idea.
>


IIRC, GNU awk does support it:

gawk -f file --source '..awk stuff' -f another_file ... data_files..

And both -f and --source options can appear several times in the same
command

I've used this technique to simulate a compile-only command:

gawk --source 'BEGIN {exit} END {exit}' -f awk_program


--
To reply by e-mail, please remove the extra dot
in the given address: m.collado -> mcollado

John DuBois

2004-05-13, 5:30 pm

In article <bob-05AEE2.21014312052004@news-central.ash.giganews.com>,
Robert Peirce <bob@peirce-family.com.invalid> wrote:
>What I would like to know is if anybody knows a way to combine these two
>methods so that you can read the functions in from a file but have the
>script entered as in-line code. For example:
>
>awk -f fun.awk '{
> in-line-code
>}'
>
>This does not work, but you get the idea.


gawk:

gawk -f fun.awk --source '{
in-line-code
}'

Some other awks have similar options - see your awk man page:

awk -f fun.awk -e '{
in-line-code
}'

If your OS has /dev/fd:

awk -f fun.awk -f /dev/fd/3 3<<\END
{
in-linec-ode
}
END

If you don't need to read from stdin, you can get by with /dev/stdin,
which is more common than /dev/fd:

awk -f fun.awk -f /dev/stdin <<\END
{
in-line-code
}
END

In ksh, again only if you have /dev/fd:

awk -f fun.awk -f <(print -r -- '{
in-line-code
}')

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
Robert Peirce

2004-05-13, 10:30 pm

In article <40a3daed$0$431$8eec23a@newsreader.tycho.net>,
spcecdt@deeptht.armory.com (John DuBois) wrote:

>
> gawk:
>
> gawk -f fun.awk --source '{
> in-line-code
> }'
>
> Some other awks have similar options - see your awk man page:
>
> awk -f fun.awk -e '{
> in-line-code
> }'
>
> If your OS has /dev/fd:
>
> awk -f fun.awk -f /dev/fd/3 3<<\END
> {
> in-linec-ode
> }
> END
>
> If you don't need to read from stdin, you can get by with /dev/stdin,
> which is more common than /dev/fd:
>
> awk -f fun.awk -f /dev/stdin <<\END
> {
> in-line-code
> }
> END
>
> In ksh, again only if you have /dev/fd:
>
> awk -f fun.awk -f <(print -r -- '{
> in-line-code
> }')
>
> John


Great ideas. Thanks.

--
Robert B. Peirce, Venetia, PA 724-941-6883
bob AT peirce-family.com [Mac]
rbp AT cooksonpeirce.com [Office]

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com