Questions tagged [pipe]

A pipe is an interprocess connection between file descriptors of two processes. A pipe is created with the POSIX pipe() function (from ). Shells create pipes between processes if the "|" symbol is used: "cmd1 | cmd2" directs the output of cmd1 to the input of cmd2. On Windows use CreatePipe(). This mechanism redirects standard input, standard output, and standard error into the calling process in .NET and Java.

Every program that runs on the command line has three data streams connected to it: STDIN(0) - standard input, STDOUT (1) - standard output - data printed by the program, STDERR(2) - standard error.

There are ways to connect streams between programs and files called piping and redirections.

Piping is a mechanism for sending data from one program to another using the "|" operator. The operator feeds the output from the program on the left as input to the program on the right.

Example

$ cat two_columns
column1:cloth
column2:strawberries
column3:fish
column4:chocolate
column5:punch cards
$ cat two_columns | awk -F: '{print $1}'
column1
column2
column3
column4
column5 
$ cat two_columns | awk -F: '{print "HAS: " $2}'
HAS: cloth
HAS: strawberries
HAS: fish
HAS: chocolate
HAS: punch cards 

Useful Links

9809 questions
36
votes
4 answers

Communicate multiple times with a process without breaking the pipe?

It's not the first time I'm having this problem, and it's really bugging me. Whenever I open a pipe using the Python subprocess module, I can only communicate with it once, as the documentation specifies: Read data from stdout and stderr, until…
Manux
  • 3,643
  • 4
  • 30
  • 42
36
votes
3 answers

Is it OK to use the same input file as output of a piped command?

Consider something like: cat file | command > file Is this good practice? Could this overwrite the input file as the same time as we are reading it, or is it always read first in memory then piped to second command? Obviously, I can use temp files…
Amro
  • 123,847
  • 25
  • 243
  • 454
35
votes
8 answers

Angular 2 "time ago" pipe

I am trying to create a 'time ago' pipe for my Angular 2 application. It should transform a date to a string such as '5 minutes ago' or '60 seconds ago'. It works nicely so far, but it doesn't update after the first calculation. If the given date is…
user2611144
  • 413
  • 1
  • 4
  • 9
34
votes
5 answers

How do you read from stdin in python from a pipe which has no ending

I've problem to read from Standard input or pipe in python when the pipe is from a "open" (do not know right name) file. I have as example pipetest.py: import sys import time k = 0 try: for line in sys.stdin: k = k + 1 print…
Janne
  • 343
  • 1
  • 3
  • 4
34
votes
9 answers

piping data into command line php?

It is possible to pipe data using unix pipes into a command-line php script? I've tried $> data | php script.php But the expected data did not show up in $argv. Is there a way to do this?
user151841
  • 17,377
  • 29
  • 109
  • 171
34
votes
6 answers

RxJS 6 - Cancel / End a Pipe

Working with the new version of RxJS 6 and the pipe operator in particular. Currently using the pipe to take the results of an API call and pass them to a series of additional tasks. All works great, but can't seem to find a way to cancel or end a…
RookieMcRookie
  • 403
  • 2
  • 5
  • 7
34
votes
2 answers

How to use pipe within -exec in find

Is there any way to use pipe within an -exec in find? I don't want grep to go through whole file, but only through first line of each file. find /path/to/dir -type f -print -exec grep yourstring {} \; I tried to put the pipelines there with "cat"…
beranpa8
  • 435
  • 1
  • 5
  • 11
34
votes
11 answers

blocks - send input to python subprocess pipeline

I'm testing subprocesses pipelines with python. I'm aware that I can do what the programs below do in python directly, but that's not the point. I just want to test the pipeline so I know how to use it. My system is Linux Ubuntu 9.04 with default…
nosklo
  • 217,122
  • 57
  • 293
  • 297
33
votes
2 answers

Connecting n commands with pipes in a shell?

I am trying to implement a shell in C. I can execute simple commands just fine with a simple execvp() but one of the requirements is to manage commands like this: "ls -l | head | tail -4" with a 'for' loop and only one 'pipe()' statement redirecting…
vicpermir
  • 3,544
  • 3
  • 22
  • 34
33
votes
5 answers

Checking for interactive shell in a Python script

I need to determine whether the shell which invoked my Python script was in interactive mode or not. If it was in interactive mode, the program should pipe output to less(1) for easy reading. If not, it should simply print its output to stdout, to…
jforberg
  • 6,537
  • 3
  • 29
  • 47
33
votes
5 answers

How to detect if a Node.js script is running through a shell pipe?

My question is similar to this one: How to detect if my shell script is running through a pipe?. The difference is that the shell script I’m working on is written in Node.js. Let’s say I enter: echo "foo bar" | ./test.js Then how can I get the…
Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248
33
votes
4 answers

Using FFMPEG to stream continuously videos files to a RTMP server

ffmpeg handles RTMP streaming as input or output, and it's working well. I want to stream some videos (a dynamic playlist managed by a python script) to a RTMP server, and i'm currently doing something quite simple: streaming my videos one by one…
kketch
  • 673
  • 1
  • 7
  • 10
32
votes
5 answers

link several Popen commands with pipes

I know how to run a command using cmd = subprocess.Popen and then subprocess.communicate. Most of the time I use a string tokenized with shlex.split as 'argv' argument for Popen. Example with "ls -l": import subprocess import shlex print…
user940797
31
votes
3 answers

Bash Pipe Handling

Does anyone know how bash handles sending data through pipes? cat file.txt | tail -20 Does this command print all the contents of file.txt into a buffer, which is then read by tail? Or does this command, say, print the contents of file.txt line by…
num1
  • 4,825
  • 4
  • 31
  • 49
31
votes
5 answers

Pipe | Redirection < > Precedence

I want to make clear when does pipe | or redirection < > takes precedence in a command? This is my thought but need confirmation this is how it works. Example 1: sort < names | head The pipe runs first: names|head then it sorts what is returned…
Kairan
  • 5,342
  • 27
  • 65
  • 104