Is there a unix command line command to "match" line by line?

I have an input stream and I want to "match" with the pins. For example, if my input stream was a nums file, I would like this syntax

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

will be equivalent

 $ echo 9534 | ./myscript $ echo 2343 | ./myscript $ echo 1093 | ./myscript $ echo 7023 | ./myscript 
+6
source share
4 answers

I think xargs is closest 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 does not allow you to refer to things that are read from stdin, so you will have to rewrite your script to accept the command line argument and not read from stdin.

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

You can use a bash script for this. Sort of:

 #!/bin/bash string="| ./myscript" echo "9534 $string" echo "2343 $string" echo "1093 $string" echo "7023 $string" 

Or you can easily populate the array with your numerical values ​​and just do it all in one loop.

0
source

With GNU Parallel, it looks like this:

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

which is equivalent

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

Or you can do:

 $ cat nums | parallel ./myscript 

which is equivalent

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

Work will be performed in parallel (one task per core) until all tasks are completed.

0
source

Source: https://habr.com/ru/post/911708/


All Articles