ssb
ssb

Reputation: 7502

Replacing every element in a matrix

I have a table of numbers like this --

13030 11537 40387 38
31500 174 40387 38
8928 7132 40387 40387 40387 40387 38
1299 174 40387 38

All the rows dont have the same number of columns.

I want to make another table of the same size, by replacing the each number with cmd $number where cmd is a generic bash command (may be piped). And I want to do the whole thing in bash.

Can this be done?

Upvotes: 0

Views: 43

Answers (2)

Vytenis Bivainis
Vytenis Bivainis

Reputation: 2376

I like using xargs. Replace echo %s + 1 | bc with your command. My example adds one to each number.

xargs -L 1 -i bash -c "printf 'echo -n \"\$(echo %s + 1 | bc) \";' {} ; echo 'echo;'"

Upvotes: 1

Juan Cespedes
Juan Cespedes

Reputation: 1363

The next program will do it:

while read line
do
  for num in $line
  do
    result=$( cmd $num )
    echo -n "$result "
  done
  echo
done

Upvotes: 2

Related Questions