execute a script for each line of an input file

I have a txt file with 100 lines and 5 columns. I want to automatize a script, with reading the file each lines, make the first column the first argument, the second column the second argument etc. How can solve this?

1 Answer

It's hard to be definitive without a real example from you, but you can use the array form of the bash shell's built-in read function something like:

while read -ra arr; do cmd "${arr[@]}"; done < file

Ex. given a file

$ cat file
foo some arg 3
bar otherarg 5 -o bar
baz "arg with spaces" -99

then

$ while read -ra arr; do echo cmd "${arr[@]}"; done < file
cmd foo some arg 3
cmd bar otherarg 5 -o bar
cmd baz "arg with spaces" -99

Alternatively, if your command arguments are simple (no quoted spaces for example) then you could try xargs e.g.

xargs -L1 -a file cmd

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like