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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hitting return once does not execute awk script 2

Status
Not open for further replies.

pter

Technical User
Jul 30, 2007
2
SK
Hello Forum,
I work under linux kernel 2.6.20-1.2320.fc5
I wrote an awk script (named prim_fail) for generating a sequence of draws from a binomial distribution:

Code:
BEGIN {
        srand()
}
{       for(i=1;i<=100;i++)
        {       if (rand()<0.2)
                        print $1=1
                else
                        print $1=0
        }
        exit
}


I run the script as follows:
$ awk -f prim_fail > sim_sheet
After entering the command, I press "return" (cursor jumps to the next line) , but I need to press "return" again to get to the shell prompt again. This is not convenient when running this awk script from within a python script loop, because for every iteration I need to press the "return" key.
Can anyone give me a tip how to avoid pressing "enter" manually? It seems that this problem does not occur when awk gets data from a file: The problem occurs when the data generated by awk are written to a newly created file.
Thank you,
pter
 
Hi

Your script has a block which will be executed for every input record. In the command line you do not specify any input file, so [tt]awk[/tt] will read the standard input.

What is your goal ? How will you run that script from [tt]python[/tt] ?

Feherke.
 
My goal is to create a column of 0s and 1s and write it to a file (which works by hitting return twice). Once I have this file, I would like to process it with short awk scripts further (by adding more columns of data).
Finally, I would like collect some statistics from the resulting file (in the python script below denoted as sim_sheet7), and append the stats report to another file (in the python script below denoted as test2)
Here is the python script:
Code:
#!/usr/bin/python
import os
for i in range(10):
        os.system("awk -f   prim_fail  > sim_sheet")
        os.system("awk -f   back_fail  sim_sheet > sim_sheet2")
        os.system("awk -f   check_freq  sim_sheet2 > sim_sheet3")
        os.system("awk -f   backup_state  sim_sheet3 > sim_sheet4")
        os.system("awk -f   system_state  sim_sheet4 > sim_sheet5")
        os.system("awk -f   backup_found_failed  sim_sheet5 > sim_sheet6")
        os.system("awk -f   multi_fail  sim_sheet6 > sim_sheet7")
        os.system("awk -f   sim_sum sim_sheet7 >> test2")

...and now I need to hit enter ten times to execute the python script loop.

pter
 
Try putting all your code in the BEGIN block, then awk doesn't need an input file and won't read from stdin either...

Code:
BEGIN {
        srand()

        for(i=1;i<=100;i++)
        {       if (rand()<0.2)
                        print $1=1
                else
                        print $1=0
        }
        exit
}


HTH,

p5wizard
 
Hi

The simplest way is to modify the first line of execution, according to what you need :
Code:
os.system("awk -f prim_fail [red]/dev/null[/red] > sim_sheet")

[gray]# or[/gray]

os.system("[red]echo |[/red] awk -f prim_fail > sim_sheet")
The complicated way is to make us understand what is your goal.

Sorry, what you are doing there is just stupid. You use a lot of temporary files and a lot of execution of external commands, which slows down the script. At least execute them all in one :
Code:
#!/usr/bin/python
import os
for i in range(10):
        os.system("awk -f prim_fail | awk -f back_fail | awk -f check_freq | awk -f backup_state | awk -f system_state | awk -f backup_found_failed | awk -f multi_fail | awk -f sim_sum >> test2")
But the best would be to do it all in [tt]python[/tt].
pter said:
My goal is to create a column of 0s and 1s and write it to a file
Then why not simply create them ?
Code:
BEGIN {
  srand()
  for(i=1;i<=100;i++) print rand()<0.2
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top