Question

I have an input stream, and I want to "map" to output lines. For instance, if my input stream were the files nums, I'd want that this syntax

$ cat nums
9534
2343
1093
7023
$ cat nums | map ./myscript $0

would be equivalent to

$ echo 9534 | ./myscript
$ echo 2343 | ./myscript
$ echo 1093 | ./myscript
$ echo 7023 | ./myscript
Was it helpful?

Solution

I think xargs is the closest thing to your hypothetical map:

cat nums | xargs -n1 ./myscript

or

cat nums | xargs -n1 -J ARG ./myscript ARG

or

cat nums | xargs -I ARG ./myscript ARG

Unfortunately, xargs doesn't let you invoke things that read from stdin, so you'd have to rewrite your script to accept a command-line argument rather than reading from stdin.

OTHER TIPS

#!/bin/bash
while read -r line
do
  "$@" <<< "$line"
done

You could use a bash script to do this. Something like:

#!/bin/bash

string="| ./myscript"

echo "9534 $string"
echo "2343 $string"
echo "1093 $string"
echo "7023 $string"

Or, you could easily fill an array with your numerical values and just do this all in one loop.

With GNU Parallel it looks like:

$ cat nums | parallel -N1 --pipe ./myscript

which is equivalent to

$ echo 9534 | ./myscript
$ echo 2343 | ./myscript
$ echo 1093 | ./myscript
$ echo 7023 | ./myscript

Or you can do:

$ cat nums | parallel ./myscript

which is equivalent to

$ ./myscript 9534
$ ./myscript 2343
$ ./myscript 1093
$ ./myscript 7023

The jobs will run in parallel (one job per core) until all jobs are done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top