How to give a text file as input while running a program?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to give a text file as input while running a program?
# 1  
Old 01-14-2013
How to give a text file as input while running a program?

Hi Friends,

I am running a program

Code:
/path/to/program -i 1 100 -o /path/to/output/op_1_100.txt

In the above command, I have to try various number of combinations at the -i parameter and the output file name varies with each combination.

Now, I have my -i parameter text file, which is like this

Code:
cat input.txt
1 101
1 150
1 235
1 600

and the corresponding output file names in another file called output.txt

Code:
cat output.txt
op_1_101.txt
op_1_150.txt
op_1_235.txt
op_1_600.txt

So, I would like to run my command this way

Code:
/path/to/program -i /path/to/input.txt -o /path/to/output.txt

Any pointers are highly appreciated towards this issue.
# 2  
Old 01-14-2013
Assuming that all output file names follow the same pattern as in your example, you don't need output.txt (the output file names can easily be created from input.txt). I use the Korn shell, but this will also work with bash, or any other shell that accepts basic Bourne shell syntax:
Code:
#!/bin/ksh
while read x y
do      echo /path/to/program -i "$x" "$y" -o /path/to/output/"op_$x_$y.txt"
done < input.txt

Try it with the echo to be sure it creates the commands you want to run; then remove the echo and rerun this script to run your program once for each line in input.txt.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 01-14-2013
Quote:
Originally Posted by Don Cragun
Assuming that all output file names follow the same pattern as in your example, you don't need output.txt (the output file names can easily be created from input.txt). I use the Korn shell, but this will also work with bash, or any other shell that accepts basic Bourne shell syntax:
Code:
#!/bin/ksh
while read x y
do      echo /path/to/program -i "$x" "$y" -o /path/to/output/"op_$x_$y.txt"
done < input.txt

Try it with the echo to be sure it creates the commands you want to run; then remove the echo and rerun this script to run your program once for each line in input.txt.
Hi Don,

Thanks for the script.

But the $x value is not being printed in the output file name.

And also, my output file names might vary and they should be read from the output.txt file.

Any suggestions?
# 4  
Old 01-14-2013
The $x prints in both places when I run it; but if you want the names from the second file instead of using the pattern present in your sample files, the following should work as long as lines in input.txt and output.txt are in the same order and there are at least as many lines in output.txt as there are in input.txt:
Code:
#!/bin/ksh
while read x y
do      read of <&3
        echo /path/to/program -i "$x" "$y" -o /path/to/output/"$of"
done < input.txt 3< output.txt

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-14-2013
Quote:
Originally Posted by Don Cragun
The $x prints in both places when I run it; but if you want the names from the second file instead of using the pattern present in your sample files, the following should work as long as lines in input.txt and output.txt are in the same order and there are at least as many lines in output.txt as there are in input.txt:
Code:
#!/bin/ksh
while read x y
do      read of <&3
        echo /path/to/program -i "$x" "$y" -o /path/to/output/"$of"
done < input.txt 3< output.txt

Thats all I need Don!

Thanks for your time and a perfect solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to give input once logged in to server in script

Hi , when i am logging to the server i need to give input of specific key like k or l or m etc. and then need to put enter. need to use this in script . please assist. (1 Reply)
Discussion started by: rupesh.bombale
1 Replies

2. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

3. Shell Programming and Scripting

Take name of input file to give the name of output file

Hello everyone, I think this would be easy for you experts. Refering the suggestion that Scrutinizer gave me in this thread https://www.unix.com/302612499-post13.html I have the script in this form awk '{$1=$1}1' "$1" | awk ' ...' Where "$1" takes the value of "inputfile" then I can... (1 Reply)
Discussion started by: Ophiuchus
1 Replies

4. Shell Programming and Scripting

How to give runtime arguments to different program

I have a shell script that is attempting to call a c program. I call the c program with ./dictool dictool accepts arguments at runtime. It works by prompting the user for various commands, acting on those commands, spitting out an output, and then prompting for more commands. My question is,... (1 Reply)
Discussion started by: aarongoldfein
1 Replies

5. Programming

Please give me some advise to program for unix/linux using c/c++?

I have a good foundation of c++.I want to learn to program for linux/unix,can you give me some advises,for example classic books ,which operating system is used better(freebsd,solaris,federal linux.etc),and which aspects uses mostly in job.Can you give me clear direction for working or learning. (1 Reply)
Discussion started by: fengshuiyue
1 Replies

6. Shell Programming and Scripting

Can process substitution be used as an input file to another program?

Hey, I was trying to figure out how to launch a program from the command line, and it works if you pass it a config file. I was thinking about writing a script to dynamically create the config file and pass it to the command using something like command substitution (so I don't actually have to... (3 Replies)
Discussion started by: bj0
3 Replies

7. Shell Programming and Scripting

How to run this program with only one input file at a time

i have a program ABC, which runs every two minutes and takes the input according to the a value called "pointer" files need to be processed by ABC are input0001 input0002 input0003 input0004 input0005 current value of pointer is 0001, now program runs and takes all the files from... (2 Replies)
Discussion started by: Prat007
2 Replies

8. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

9. Shell Programming and Scripting

File path with space as external input to the program

Hello I am getting error when the file (Folder or Application) path having space is given as external input to the shell program. It works fine for the files which has no spaces in the file name Thans, (5 Replies)
Discussion started by: keshav.murthy@r
5 Replies

10. HP-UX

get program name give a process id

Hi , I have query regarding to get a program name given a pid in HP-Ux . give procees id ( PID) i would like to retrieve the process/program through a C program ? my input to c program will be will be pid and i would like to know what is process name /program name . Many Thanks ... (1 Reply)
Discussion started by: naren_chella
1 Replies
Login or Register to Ask a Question