2

I have a single-thread python script that gets used like this:

python -c "arg"

I also have a *.txt file with many arg, each in one single line in the respective *.txt. A text file, say 1.txt looks as follows:

arg1
arg2
arg3
arg4

Now I want to run the python script with all this arguments, one by one as the script does not allow more than one argument.

How can I process this arguments in batch mode using commandline?

0

2 Answers 2

1

One way:

for i in *.txt
do
   while read line
   do
     python -c "$line"
   done < $i
done
3
  • Not working. The python script takes the text file as argument, not its content.
    – mcbetz
    Jan 10, 2013 at 13:33
  • @mmb : updated the solution
    – Guru
    Jan 10, 2013 at 13:57
  • Totally does the job now!
    – mcbetz
    Jan 10, 2013 at 14:54
1

A very easy solution,

cat 1.txt | xargs -I{} python -c "{}"

Explanation: cat prints all lines and xargs makes sure each line is used as argument.

Edit: the -I{} means that {} should by replaced by the argument.

2
  • Wow, that is indeed easy. But does it take into consideration, that the arguments have to be enclosed by ""?
    – mcbetz
    Jan 10, 2013 at 14:56
  • Now it does, see my edit ;) If you have multiple files, simply replace 1.txt by *.txt and it should work as well. Jan 10, 2013 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .