-1

General description:

I had a couple of times the same issue, that I solved by doing an step by step script. But I know that with pipe, one-liners would make my life way easier.

Simply:

command1 -flag target.file | command2 -flag 

As far as I understand, the command2 is applied over the output of command1 -flag target.file. However, command2 has an extra argument that has to be added at the end. So, it would be read as:

command2 -flag OUTPUTOFCOMMAND1 argument2

When I try to pipe directly with |, of course I don't get the desired command, as it passes the output to the end of the second instruction. I'd like to redirect the piped output to an specific part of the second instruction. Something like this:

command1 -flag target.file | command2 -flag PIPEOUTPUT1HERE argument2

I've found a similar question in here: Pass the output of previous command to next as an argument, but i would like a more general, and simple approach. As I have used, without sucess:

command1 | xargs -I{} command2 {}

Which is basically doing command2 $(command1), but I would like to pipe the entire output of it. Like a simple ls | grep "something"

My specific case:

I want to take out the first line of a dat file, and split it every 11 lines:

$ tail -n +2 ac_sweep_m+_-30.00mT.rx | xargs -I{} split -l 2 {} 

But passes every line of the previous output at theend of the second instruction:

"ac_sweep_m+_-30.00mT_"              
split: cannot open '0.000000e+00 0.000363094 -0.000518363 -0.000389763 0 ' for reading: No such file or directory
split: cannot open '2.800000e-11 -0.0853443 -0.00230621 -0.000370474 0 ' for reading: No such file or directory
split: cannot open '6.700000e-11 -0.205325 -0.00375738 0.000241809 0 ' for reading: No such file or directory

How can I direct the pipe exactly into an specific position?

  • This doesn't make sense, the pipe doesn't care what arguments the other command gets, it just connects the stdout of the program on the left to the stdin of the program on the right. The output doesn't appear on the command line, as that's different. The only thing there is that the program on the right has to read its stdin, but telling it how to do that (if necessary) isn't related to the pipe as such. – ilkkachu Jun 09 '22 at 09:56
  • I doubt that you have completely understood piping. It connects stdout of the first command to stdin of the second command. Your »solution« only works if the second command supports to take the desired part from stdin. – Philippos Jun 09 '22 at 09:57

1 Answers1

0

The solution was simpler than I expected:

To do this:

command1 -flag target.file | command2 -flag PIPEOUTPUT1HERE argument2

I used simply- to direct properly the output of the first command:

command1 -flag target.file | command2 -flag - argument2

In my case, it works perfectly. Cuts the first line of a dat file, and splits the file into pieces of 21 lines each:

$ tail -n +2 ac_sweep_m+_-30.00mT.rx | split -l 21 - "ac_sweep_m+_-30.00mT_"
$ ls
ac_sweep_m+_-30.00mT_aa  ac_sweep_m+_-30.00mT_ae  ac_sweep_m+_-30.00mT_ai  ac_sweep_m+_-30.00mT_am  ac_sweep_m+_-30.00mT_aq  
ac_sweep_m+_-30.00mT_ab  ac_sweep_m+_-30.00mT_af  ac_sweep_m+_-30.00mT_aj  ac_sweep_m+_-30.00mT_an  ac_sweep_m+_-30.00mT_ar
  • Not every command takes the "-" argument as "stdin". /dev/stdin or /proc/self/fd/0 might work as well. – RudiC Jun 09 '22 at 11:24