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 about it without breaking the pipe.
for example
> echo "1 2 3 4 5" | some command | cat
1 4 9 16 25
>
Would you have any ideas on how to make something like this work? The actual operation I want to perform is sim开发者_StackOverflowply adding one to every number.
echo 1 2 3 4 5|{
read line;
for i in $line;
do
echo -n "$((i * i)) ";
done;
echo
}
The {} creates a grouping. You could instead create a script for that.
I'd write:
echo "1 2 3 4 5" | {
for N in $(cat); do
echo $((N ** 2))
done | xargs
}
We can think of it as a "map" (functional programming). There are a lot of ways of writing a "map" function in bash (using stdin, function args, ...), for example:
map_stdin() {
local FUNCTION=$1
while read LINE; do
$FUNCTION $LINE
done
}
square() { echo "$(($1 * $1))"; }
$ echo "1 2 3 4 5" | xargs -n1 | map_stdin square | xargs
1 4 9 16 25
Or..
echo "1 2 3 4 5" | xargs -n 1 | while read number
do
echo $((number * number))
done
echo 1 2 3 4 5 | xargs -n 1 expr -1 +
echo 1 2 3 4 5 | xargs -n 1 bash -c 'echo $(($1*$1))' args
Using awk is another solution, which also works with floats
echo "1 2 3 4 5" | xargs -n1 | awk '{print $1^2}' | xargs
or use a loop
for x in 1 2 3 4 5; do echo $((x**2)); done | xargs
for x in $(echo "1 2 3 4 5"); do echo $x^2 | bc; done | xargs # alternative solution
for x in $(seq 5); do python -c "print($x**2)"; done | xargs # alternative solution but slower than the above
# or make it neat by defining a function to do basic math in bash, e.g.:
calc() { awk "BEGIN{print $*}"; }
for x in $(seq 5); do calc $x^2; done | xargs
Or you can pipe to expression to bc:
echo "1 2 3 4 5" | (
read line;
for i in $line;
do
echo $i^2 | bc;
done;
echo
)
If you prefer Python:
#!/bin/python
num = input()
while num:
print(int(num) + 1) # Whatever manipulation you want
try:
num = input()
except EOFError:
break
xargs, xargs, xargs
echo 1 2 3 4 5 | xargs -n1 echo | xargs -I NUMBER expr NUMBER \* NUMBER | xargs
Or, go parallel:
squareit () { expr $1 \* $1; }
export -f squareit
echo 1 2 3 4 5 | xargs -n1 | parallel --gnu squareit | xargs
Which would be way simpler if you passed your pipe as a standard set of args:
parallel --gnu "expr {} \* {}" ::: $(echo 1 2 3 4 5) | xargs
Or even:
parallel --gnu "expr {} \* {}" ::: 1 2 3 4 5 | xargs
Really worth taking a look at the examples in the doc: https://www.gnu.org/software/parallel/man.html
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 / /, $_'
精彩评论