alecthomas/pawk

[new feature] add action(s) before the output evaluation on each line

wavefancy opened this issue · 2 comments

Hi,

Can you please add a feature to evaluate action(s) on each line before the output evaluation. For example, if I want to exchange two columns, I have to list the order in full, in the current implementation.

printf '%s ' {1..5} | pawk 'f[0],f[2],f[1],f[3],f[4]'
1 3 2 4 5

something like this:

printf '%s ' {1..5} | pawk -a 'f[2],f[1] = f[1],f[2]' 'f'

This is very convenient if we have too many columns. Or we just want to have a minor modification on each line and output results.

Thanks much!

The same solution can be achieved with:

printf '%s ' {1..5} | pawk 'f = list(f); f[2], f[1] = f[1], f[2]; f'

Currently f is a tuple and so is not mutable, and must be converted to a list beforehand. If f were instead a list this example would be:

printf '%s ' {1..5} | pawk 'f[2], f[1] = f[1], f[2]; f'

Which is of a similar succinctness. Maybe the solution then is to make f a list.