How to... 
Author Message
 How to...

How can I print say "KEYWORD" on the very first line of a configuration file?  I know I can do:-

printf "KEYWORD\n"

The problem is this file get's updated continuously.   So what I end up with is:-

KEYWORD
TCP|*|*|*
KEYWORD
TCP|*|*|*
KEYWORD
TCP|*|*|*

What I need is everytime the file get's updated that the printf statement just overwrites KEYWORD on
line 1. i.e

KEYWORD
TCP|*|*|*
TCP|*|*|*
TCP|*|*|*

I have included my awk script just in case.

#!/bin/awk -f
BEGIN   {
        FS="=";
        OFS="|"

Quote:
}

NR==1 {
        print "!" $2;
        next
Quote:
}

NF>1 {
        foo[counter++] = $2
Quote:
}

END {
        printf "TCP|"
        for (i = 0; i < counter; ) {
                printf "%s", foo[i++]
                if (i == counter) continue
                printf "|"
        }
        printf "\n"

Quote:
}

---
Thanks for any help.

Regards

Michael.



Mon, 05 Nov 2001 03:00:00 GMT  
 How to...

Quote:

> How can I print say "KEYWORD" on the very first line of a
> configuration file?  I know I can do:-

> printf "KEYWORD\n"

> The problem is this file get's updated continuously.   So what I end
> up with is:-

> KEYWORD
> TCP|*|*|*
> KEYWORD
> TCP|*|*|*
> KEYWORD
> TCP|*|*|*

> What I need is everytime the file get's updated that the printf
> statement just overwrites KEYWORD on line 1. i.e

> KEYWORD
> TCP|*|*|*
> TCP|*|*|*
> TCP|*|*|*

> I have included my awk script just in case.

> #!/bin/awk -f
> BEGIN   {
>         FS="=";
>         OFS="|"
> }
> NR==1 {
>         print "!" $2;
>         next
> }
> NF>1 {
>         foo[counter++] = $2
> }
> END {
>         printf "TCP|"
>         for (i = 0; i < counter; ) {
>                 printf "%s", foo[i++]
>                 if (i == counter) continue
>                 printf "|"
>         }
>         printf "\n"
> }

It doesn't look like you're telling the whole story. Aside from the
first line in the configuration file (NR==1), there's nothing in the
awk script that would print other lines that didn't contain two or more
fields. Since FS is set to '=', that means after the first line, is a
subsequent line didn't contain a '=' it wouldn't be written to the
output/processed configuration file. The printf call in your END action
doesn't print '=' characters (unless they're in foo[i]), so it doesn't
look like you're saving the awk script output in the configuration file.

Does the update procedure overwrite the configuration file? Are you
maintaining script-processed configuration files in another file (which
I'll call an archive file) and adding the output from this script to
the archive file? That would explain the multiple KEYWORD lines.

I'd guess you're doing this as

awkscript config_file >> config_archive

If the KEYWORD line is based on information from the newly updated
configuration file, but you want the new data from that file added to
the end of the archive, you need to do some tricky I/O processing.

new command line:
awkscript -v archive=config_archive config_file

rewritten script:
#!/bin/awk -f
BEGIN {
  FS="=" # OFS unneeded

Quote:
}

NR==1 {
  print "!" $2
  getline < archive # eat the first line
  while (getline < archive) print
  close(archive)
  next
Quote:
}

NF>1 {
  foo[++counter] = $2  # note pre-increment
Quote:
}

END { # nearly full rewrite
  printf "TCP|"
  for (i = 1; i <= counter; ++i) {
    printf "%s%s", foo[i], (i < counter) "|" : "\n"

Quote:
}

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


Mon, 05 Nov 2001 03:00:00 GMT  
 How to...
On 20 May 1999 15:42:40 GMT, Michael Haines - Sun UK
Quote:

> How can I print say "KEYWORD" on the very first line

...

Hi,
  I can't understand what you mean, not even from your
script.  It might help if you describe your problem in whole,
not only the particular question.  Or construct an excercise
analogic to your real problem to avoid bothering with unnecessary
details.
                        Bye for now,    Stepan Kasal



Mon, 05 Nov 2001 03:00:00 GMT  
 How to...
In an e-mail message dated 5/21/99, 4:47:07 AM,

<snip>

Quote:
>What I am doing is writing a file out (based on user input) then
>running my awk script inside the actual script.

>>Does the update procedure overwrite the configuration file?

>No, it appends to it.

<snip>

Quote:
>Here is what actually happens:-  I will run this script twice so you
>can see what is happeing.

>First time of running the script and passing the output file to the awk
>script:-

Begin input to awk script?

Quote:
>Create SMTP access and relay restriction rules...

>Format: [TCP|server-address|server-port|client-address|client-port]

>Enter the comment: [comment] foo1
>setting: comment=foo1

>Enter the server_address: [*] 1
>setting: server_address=1

>Enter the server_port: [*] 1
>setting: server_port=1

>Enter the client_address: [*] 1
>setting: client_address=1

>Enter the client_port: [*] 1
>setting: client_port=1

>No mappings file to backup!...
>Now reformating the mappings configuration file...
>Creating New IMTA mappings configuration file...
>Cleaning up temporary files...

End input to awk script?

Quote:
>hostname:/$ more mappings
>KEYWORD
>!foo1
>TCP|1|1|1|1

And this (excluding the /^hostname/ line) is the output from the awk
script the 1st time?

Quote:
>THE ABOVE IS RIGHT - THIS IS WHAT I WANT.

>Second time of running the script and passing the output file to the
>awk script:-

Begin input to awk script?

- Show quoted text -

Quote:
>Create SMTP access and relay restriction rules...

>Format: [TCP|server-address|server-port|client-address|client-port]

>Enter the comment: [comment] foo2
>setting: comment=foo2

>Enter the server_address: [*] 2
>setting: server_address=2

>Enter the server_port: [*] 2
>setting: server_port=2

>Enter the client_address: [*] 2
>setting: client_address=2

>Enter the client_port: [*] 2
>setting: client_port=2

>Taking a snapshot copy of the current mappings.conf file...
>Taking a snapshot copy of the current mappings file...
>Now reformating the mappings configuration file...
>Creating New IMTA mappings configuration file...
>Cleaning up temporary files...

End input to awk script?

Quote:
>hostname:/$ 71 $ more mappings
>KEYWORD
>!foo1
>TCP|1|1|1|1
>KEYWORD         <----------- MUST NOT APPEAR
>!foo2
>TCP|2|2|2|2

And this (excluding the /^hostname/ line) is the output from the awk
script the 1st time?

If so, your problem is due to appending awk script output.

Quote:
>HERE YOU CAN SEE THAT THE KEYWORD HAS BEEN INSERTED AGAIN! THE WORD
>KEYWORD MUST ONLY APPEAR ON THE FIRST LINE!

>So, by putting the print "KEYWORD" statement in my awk script it just
>seems to duplicate this word.

>Does this make sense now?

Yes. Your assessment of the problem is correct. One way to handle this:
initialize the file in which you're storing the output from your awk
script so that before any configuration data is written to it it
contains KEYWORD on the first line followed by a newline and nothing
else. Then DON'T print KEYWORD from your awk script.

The script you posted to comp.lang.awk didn't include a line to print
KEYWORD, so you failed to give crucial details.

I'll post this to comp.lang.awk as well.

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



Tue, 06 Nov 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 
 

 
Powered by phpBB® Forum Software