I inherited a tcsh "master" script that works and extracts lines from files with data with using 'zcat' piped to 'grep' and some other commands/scripts. The extracted lines contain numbers . It is getting difficult to do the calculations that I need to do on the numbers in the tcsh script , thus I am looking for an alternative that reuses the working grep and other commands .

I could easily do the calculations in C . Getting the numbers in the program without creating lots of intermediate files is the challenge , as is doing pipes from the output of one command into another .

How can I transform the "master" script to a perl , ruby or python script ? I did not really find the examples in perl and ruby that much more readable than the original tcsh script . In python the os.system calls do not give the contents back , just an exit status , the same problem I would have with using C .

Is there a better way than using os.system in python to get information from programs, scripts or program combinations? Or should I take a closer look at perl or ruby?

  • Welcome to Unix & Linux. Unfortunately this isn't a very good question for this site as it's very broad. If you asked about how to do a specific action in a script, that would be acceptable. But converting an entire script is not, especially when we haven't been provided with the script to see what it's doing. If you wish to edit this question, or delete it and ask a more specific one, that would be better. – phemmer Sep 8 '14 at 12:45
  • As Patrick explained, your question is not really answereable in its current form and I am closing it as "too broad". It can be reopened if you edit to provide the specific script you are talking about. However, note that simply pasting the script and expecting us to translate for you is also off topic. A better way might be to break the problem into small parts and ask questions about each of them. – terdon Sep 8 '14 at 13:07

Using os.system() in Python to get the output from calling a command is not the way to go. For single commands you can use the function check_output() from the subprocess module.

In your situation I would take a look at plumbum it allows you to do things in python like:

from plumbum.cmd import zcat, grep
chain = zcat["your_file_name.gz"] | grep["-i", "pattern"]
result = chain()

And then get the numbers you need from the result variable (a string).

You will need to install plumbum using pip or easy_install

Not the answer you're looking for? Browse other questions tagged or ask your own question.