BASH: how to perform arithmetic on numbers in a pipe

前端 未结 10 2040
忘掉有多难
忘掉有多难 2021-01-18 08:12

I am getting a stream of numbers in a pipe, and would like to perform some operations before passing them on to the next section, but I\'m a little lost about how I would go

10条回答
  •  没有蜡笔的小新
    2021-01-18 08:20

    Yoi might like something like this:

    echo "1 2 3 4 5" | perl -ne 'print $_ ** 2, " " for split / /, $_'
    

    or even like this:

    echo "1 2 3 4 5" | perl -ne 'print join " ", map {$_ ** 2} split / /, $_'
    

提交回复
热议问题