[SOLVED] Awk: Input from one line, execute program; input from next line, execute program
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The above code does not work, as I think it would take the whole first and second fields as the input for one particular instance of the program 'inputbashangle'. What I want is to get the first two numbers from the first line of the file 'outfailtest', execute 'inputbashangle' with them, then move on to the first two numbers of the second line for all the lines of 'outfailtest'.
In scripting, commands are executed in serial fashion; in general, a command runs only after the command preceding it terminates (barring some complicated workarounds).
The usual way to proceed is to first build lists of entries you want to work on, holding them in variables or temp files, then use a separate loop to iterate through them.
In order to give you more detailed advice though, I think we'd need to see an example of the contents of the files in question, as well as what you want the actual output to look like. Your current description is a little too vague for me to understand fully.
Last edited by David the H.; 09-25-2010 at 10:11 PM.
Reason: reworded for clarity.
In scripting, commands are executed in serial fashion; in general, a command runs only after the command preceding it terminates (barring some complicated workarounds).
The usual way to proceed is to first build lists of entries you want to work on, holding them in variables or temp files, then use a separate loop to iterate through them.
In order to give you more detailed advice though, I think we'd need to see an example of the contents of the files in question, as well as what you want the actual output to look like. Your current description is a little too vague for me to understand fully.
Note: I was wrong about what was wrong in the OP. Read the end for details of what's actually wrong.
Ok. I have a simulator called '3bodyangle', which takes in two coordinates and outputs many lines of ten or so numbers each. Right now I have two scripts: the first one merely reads two numbers as input for the simulator:
Code:
more inputbashangle
#!/bin/bash
read A B
./3bodyangle "$A" "$B" 1 0.001 0.002 >> outputangle/outangle"$1"-"$2"
The second one sends the prior script to a remote cluster. This is the script mentioned in my OP.
I have two scripts because the default cluster output has some extraneous information in it (such as what the submitted job's name is, the CPU time, etc) that I do not want. The first script, 'inputbashangle', gives me the raw data. The file 'outfailtest' looks like this (apologies, the formatting is odd when copied and pasted):
This is what I need help with:
Right now, if I execute 'massinputangle' the correct number of jobs is submitted, which is one job per line in the text file 'outfailtest', which 'massinputangle' uses as input. However, I only get one output file with the information from all of the jobs, which is called 'outangle-'. So it seems that the output from each file is not getting sent to a new output file called 'outangle"$1"-"$2", but rather the "$1" and "$2" are getting ignored in the output file's name, so everything gets sent to one file. Executing the first script, 'inputbashangle', by itself gives the correct output naming (ie if A=2 and B=2, then the output file's name is outangle2-2). So it seems that it's the second script, 'massinputangle', that messes something up.
Read the section about pipes in the info gawk file, and use gawk rather that awk. In gawk. your programs would simply be gawk '{print $1 " " $2 | "./inputbashangle"}' outfailtest
(This use of a pipe is, if I correctly recall the documentation, a gawk extension. Bi-directional pipes are also available if needed.)
Note: I think your code would, perhaps, have worked if you remembered that $1 $2 concatenates those two strings into a single string. Try awk '{c="./inputbashangle " $1 " " $2;system(c)}' outfailtest
Last edited by PTrenholme; 09-25-2010 at 10:46 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.