For Programmers: Free Programming Magazines  


Home > Archive > AWK > August 2005 > Re: How to name the output file dynamically after a search result









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 Re: How to name the output file dynamically after a search result
Loki Harfagr

2005-08-12, 8:59 am

Le Fri, 12 Aug 2005 00:34:25 -0700, triangle a écrit_:

> Hi,
>
> I have got the following text saved in a file:
>
> ---
> abc123#sh run
> Building configuration...
>
> Current configuration:
> !
> version 12.0
> service timestamps debug uptime
> service timestamps log uptime
> no service password-encryption
> !
> hostname abc123
> !
> ...
> end
>
> abc123#
> ---
>
> I am using this awk script...
>
> ---
> #!/bin/awk -f
>
> /^version/ { ++ok } ok
> /^end/ { exit }
> ---
>
> ...to print only lines between (and including) the "version 12.0" line
> as well as the "end" line:
>
> ---
> version 12.0
> service timestamps debug uptime
> service timestamps log uptime
> no service password-encryption
> !
> hostname abc123
> !
> ...
> end
> ---
>
> This works quite well. Now, I would like to write this output to a file
> which is dynamically named after the hostname (here, the file name
> whould be "abc123-confg"). I don't figure out how to do that within the
> awk script. One problem is that the hostname is not yet known when the


As you say the "hostname" is part of the config filename, then
you're safe, it *is* known. With the exception of :
- file will not be empty
- not known in the BEGIN section.

> first lines of the output should be printed to the file.
>
> Any help about doing that using awk only is much appreciated.


Here's a proof of concept :-)

$ date> abc123-confg.blob
$ awk 'FNR==1{split(FILENAME,a,"-"); print a[1]}' abc123-confg.blob
abc123
$ awk 'FNR==1{split(FILENAME,a,"."); print a[1]}' abc123-confg.blob
abc123-confg

Now, I'm sure you can play with it ;-)
Loki Harfagr

2005-08-12, 4:59 pm

Le Fri, 12 Aug 2005 05:08:35 -0700, triangle a écrit_:

> Hm, that's actually not what I meant.
>
> The "hostname" is only part of the [raw] config file, not of the config
> file *name*!


My apologies !
I think I read your description too fast and took it reversed :-)
I have the small excuse of at this time being on the packing for
the bloody august summer vacation ... A bit of a stress indeed ;-)

Then, the solution to your problem is to store
the data then write it in the end, after having read the
'hostname' line.

As Ed. posted, this amount of data is small enough to get easily
in an array buffer; in case of extreme volumes you'd have the
possibility to store the data in a tempfile
(print >/tmp/tempotempo) then, in the END
make a system call to rename this tempfile.

Cheers :-)

Ed Morton

2005-08-12, 4:59 pm



Loki Harfagr wrote:
> Le Fri, 12 Aug 2005 05:08:35 -0700, triangle a écrit :
>
>
>
>
> My apologies !
> I think I read your description too fast and took it reversed :-)
> I have the small excuse of at this time being on the packing for
> the bloody august summer vacation ... A bit of a stress indeed ;-)
>
> Then, the solution to your problem is to store
> the data then write it in the end, after having read the
> 'hostname' line.
>
> As Ed. posted, this amount of data is small enough to get easily
> in an array buffer; in case of extreme volumes you'd have the
> possibility to store the data in a tempfile
> (print >/tmp/tempotempo) then, in the END
> make a system call to rename this tempfile.


I'd probably opt for reading the file twice to avoid the tmp file and
system call, e.g.:

awk 'BEGIN{ARGV[ARGC++]=ARGV[1]}
NR==FNR{ ... if ($1 == "hostname") outfile=$2"-cfg"; next}
... {print > outfile}' infile

Regards,

Ed.
Loki Harfagr

2005-08-12, 4:59 pm

Le Fri, 12 Aug 2005 09:51:39 -0500, Ed Morton a écrit_:

>
>
> Loki Harfagr wrote:
>
> I'd probably opt for reading the file twice to avoid the tmp file and
> system call, e.g.:
>
> awk 'BEGIN{ARGV[ARGC++]=ARGV[1]}
> NR==FNR{ ... if ($1 == "hostname") outfile=$2"-cfg"; next}
> ... {print > outfile}' infile


Nice enough Ed. ;-)
Though, as I stated that it'd be in "extreme" volume cases I think
I still would try and read the file only once to avoid the stress
on cpu and I/O.
I know it is discussable, a lot;
(especially when we're certain that the
"hostname" line will be in the first records)
these bad habits I have are influenced
by my usual conditions of work for some "difficult" heavy customers ;-)
This said, I'm sure that on my own machine I'd prefer to
use *your* solution :-)

Allright, I am now almost ready in my packings to go in the wild
(I mean "no computer, no connection !") a fortnight vac'. Dunno if I
can cope with such extreme, horrid and harsh conditions ...

Have a nice time, you all :D)

Sponsored Links







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

Copyright 2008 codecomments.com