Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check the output file before writing on it 1

Status
Not open for further replies.

abdu22

Programmer
Sep 13, 2005
14
FR
Hi,

I have many files to be read and then put the results in ONE Output file... The output file should have this line as first line:
Erg1 Erg2 Erg3 Erg4 Erg5

The results will be arranged below this line like this:

Erg1 Erg2 Erg3 Erg4 Erg5
12 15 20 17 11
22 55 32 11 12
10 25 85 45 23

If my script has:

BEGIN {print "Erg1 Erg2 Erg3 Erg4 Erg5"}

it will print the line (Erg1 Erg2 Erg3 Erg4 Erg5) each time I read an input file... and I'll get an output like this:

Erg1 Erg2 Erg3 Erg4 Erg5
12 15 20 17 11
Erg1 Erg2 Erg3 Erg4 Erg5
22 55 32 11 12
Erg1 Erg2 Erg3 Erg4 Erg5
10 25 85 45 23


How can I prevent the printing of the first line if it exists before in the output file??

I wish it's clear :)

 
have a look in this forum. there is nearly almost the same question asked and answered before.

but ...

why not always write to your destination file without headers and adding the header after last input file is finished?
 
abdu22, none of the versions of awk I have seem to do that? How are you running awk, can you show us the scripting that surrounds it?

[tt]$ cat file1
+000000002746.26
+000000000117.1
+000000001105
13
$ cat file2
+000000002746.26
+000000000117.10
+000000001105.00
14
$ awk 'BEGIN {print "header"} {print}' file*
header
+000000002746.26
+000000000117.1
+000000001105
13
+000000002746.26
+000000000117.10
+000000001105.00
14
$[/tt]

Annihilannic.
 
mberni & Annihilannic : Thanks for your answers...
Suppose I have three files as follows:

(XXX10IPo)

Erg1 12
Erg2 15
Erg3 20
Erg4 17
Erg5 11

(XXX20IPo)

Erg1 22
Erg2 55
Erg3 32
Erg4 11
Erg5 12

(XXX01CPo)

Erg1 10
Erg2 25
Erg3 85
Erg4 45
Erg5 23

And the srcipt:

(Script.awk)

{if ($1=="Erg1") Res1=$2;
if ($1=="Erg2") Res2=$2;
if ($1=="Erg3") Res3=$2;
if ($1=="Erg4") Res4=$2;
if ($1=="Erg5") Res5=$2;
}
END{
printf ("%3d %3d %3d %3d %3d\n", Res1, Res2, Res3, Res4, Res5)
}


$ awk -f Script.awk XXX10IPo
12 15 20 17 11
$ awk -f Script.awk XXX20IPo
22 55 32 11 12
$ awk -f Script.awk XXX01CPo
10 25 85 45 23

What I am doing now: I create an output file (OUTPUT) with the line:
Erg1 Erg2 Erg3 Erg4 Erg5

as first line and then run the script:

$ awk -f Script.awk XXX10IPo >> OUTPUT
$ awk -f Script.awk XXX20IPo >> OUTPUT
$ awk -f Script.awk XXX01CPo >> OUTPUT

$ cat OUTPUT
Erg1 Erg2 Erg3 Erg4 Erg5
12 15 20 17 11
22 55 32 11 12
10 25 85 45 23


if I put BEGIN {print "Erg1 Erg2 Erg3 Erg4 Erg5"} in my script it will print this line each time I run the script... I want that the script contains the BEGIN statement and to print the line ONLY IF the line (Erg1 Erg2 Erg3 Erg4 Erg5) doesn't exist in the OUTPUT file...

is it possible?

Thanks
 
How about this:

Code:
awk '
BEGIN { print "Erg1 Erg2 Erg3 Erg4 Erg5" }
{ Ergs[$1]=$2 }
/Erg5/ {
        for (i=1; i<=5; i++) {
                printf "%-5d",Ergs["Erg" i]
        }
        print ""
}' XXX*

It processes all the files in one go, presuming that each of them will contain Erg1 through Erg5, and when it hits Erg5 it outputs all 5 of them.

Probably overcomplicated actually, thinking about it, this will do equally well:

Code:
awk '
BEGIN { print "Erg1 Erg2 Erg3 Erg4 Erg5" }
{ printf "%-5d",$2 }
/Erg5/ { print "" }
' XXX*

Annihilannic.
 
Annihilannic : Thank you for your codes...

Just one thing may I should precise: I can't read all the files at once because I need the the results to be in a special order... I applied your first code and it gives the results like this:

Erg1 Erg2 Erg3 Erg4 Erg5
10 25 85 45 23
12 15 20 17 11
22 55 32 11 12


Where as I need the results to be like this:

Erg1 Erg2 Erg3 Erg4 Erg5
12 15 20 17 11
22 55 32 11 12
10 25 85 45 23

For that I run the script for each file seperatly....

On the other hand, my script is more complicated and long and what I put here is just an example of what I want...

Anyhow thanks for your tests :)
 
Instead of specifying XXX* as the filename, just put the three filenames in the order you want them to be processed.

Annihilannic.
 
It is better now :)

Thanks a lot Annihilannic..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top