user1308144
user1308144

Reputation: 475

select ith row and nth column and assign to variable in while loop

I have a file list.txt with two columns and n rows e.g

a1.txt bd.txt
b2.txt g6.txt
.....

I need to run a command multiple times (once for each row in list.txt), where each time the input files are $1 and $2 of that row.

My attempt to do this is below, where I wanted to specify the row as a variable and increment after each loop.

a=1
while $a<=200
do
  FILE1=$(awk 'FNR==a{$1}' ./list.txt)
  FILE2=$(awk 'FNR==a{$2}' ./list.txt)
  ./command $FILE1 $FILE2
  a=$a+1
done

Upvotes: 1

Views: 184

Answers (1)

John Zwinck
John Zwinck

Reputation: 249123

It's a lot simpler than that:

while read arg1 arg2 rest; do
  ./command $arg1 $arg2
done < list.txt

There is no need for awk, much less two awks for every line.

Upvotes: 3

Related Questions