# How to capture standard input from a piped command?

**URL:** <https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509>\
**Category:** General Usage\
**Tags:** input-output\
**Created:** [May 15, 2020, 6:30am UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509 "2020-05-15T06:30:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marc\_Sevaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marc_sevaux/32/14986_2.png) [@Marc\_Sevaux](https://discourse.julialang.org/u/Marc_Sevaux)\
**Post date:** [May 15, 2020, 6:30am UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509/1 "2020-05-15T06:30:36Z")

</div>

Apologizes if this has already been answered.  
I heavily use command line scripts and combination of piped commands to generate list of numbers that I want to “statistically” analyze by piping them to a julia script.  
To be clear and simple, let’s say that I have a file containing numbers and a julia script that calculate the sum of the numbers. I want to call my script in the following way:

```julia
cat my_file.dat | julia my_script.jl

```

and this should display the sum of the numbers.

I’ve seen that I can do that with `R` in the following way

```julia
 cat my_file.dat | R --slave -e 'x <- scan(file="stdin", quiet=TRUE); summary(x)'

```

but I would love to be able to do it with julia instead.  
Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 15, 2020, 6:35am UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509/2 "2020-05-15T06:35:44Z")

</div>

You can do

```julia
s = readlines(stdin)

```

E.g. if I put that in `stdin.jl` together with `@show s`, I get

```julia
~ dpsanders$ cat test.txt | julia stdin.jl
s = ["Hello there", "3 + 4"]

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 15, 2020, 6:38am UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509/3 "2020-05-15T06:38:35Z")

</div>

And for the sum

```julia
lines = readlines(stdin)
s = sum(parse.(Int, lines))
println(s)

```

---

<div class="post-metadata">

**Author:** ![Marc\_Sevaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marc_sevaux/32/14986_2.png) [@Marc\_Sevaux](https://discourse.julialang.org/u/Marc_Sevaux)\
**Post date:** [May 15, 2020, 6:40am UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509/4 "2020-05-15T06:40:49Z")

</div>

> [@dpsanders](#):
>
> lines = readlines(stdin) s = sum(parse.(Int, lines)) println(s)

Great, so fast 😉

---

<div class="post-metadata">

**Author:** ![Marc\_Sevaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marc_sevaux/32/14986_2.png) [@Marc\_Sevaux](https://discourse.julialang.org/u/Marc_Sevaux)\
**Post date:** [May 18, 2020, 11:46am UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509/5 "2020-05-18T11:46:45Z")

</div>

To conclude the topic, if this can help others, here is the script I’ve written to calculate basic statistics from a standard input (in csv format):

```julia
#!julia
# Basic statistics from a piped input (a list of figures in csv format)
# Take input file as column of figures, csv format
using Statistics # call the right package

precision=2 # set precision
lines = readlines(stdin) # reading lines from the standard input
s = split.(lines,",") # split lines from the input (with coma as separator)
# be careful the output is an array of arrays
(m,n) = (length(s),length(s[1])) # get dimensions
res = ones(m,n) # create an array of Floats of the right dimension
# read the input split data and fill the Float array
for j = 1:m
  for i = 1:n
    res[j,i] = parse(Float64,s[j][i])
  end
end
# for each column calculate the basic statistics
for i = 1:size(res,2)
  if size(res,2)>=2 # print column identifier for more than one column
    print("col $i ")
  end
  println("[Min:",minimum(res[:,i])," Max:",maximum(res[:,i])," Avg:",round(mean(res[:,i]),digits=precision)," Sum:",sum(res[:,i])," Std:",round(std(res[:,i]),digits=precision)," Med:",round(median(res[:,i]),digits=precision),"]")
end

```

Save this script in a file called bstats (basic stats), give rights to execute it  
Usage is very simple:

```bash
cat <mycsvfile>.csv | bstats

```

Enjoy.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 18, 2020, 3:45pm UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509/6 "2020-05-18T15:45:39Z")

</div>

You could also pass the filename as an argument to the Julia script:

`julia my_stats.jl data.csv`

Command-line arguments are stored in the variable `ARGS`.

---

<div class="post-metadata">

**Author:** ![Marc\_Sevaux](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marc_sevaux/32/14986_2.png) [@Marc\_Sevaux](https://discourse.julialang.org/u/Marc_Sevaux)\
**Post date:** [May 18, 2020, 3:53pm UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509/7 "2020-05-18T15:53:34Z")

</div>

Yes, but most of the time the input file is gathered from a long list of commands (grep, cut, sed, …). Thanks for you initial help.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 18, 2020, 4:04pm UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509/8 "2020-05-18T16:04:36Z")

</div>

You can also use the built-in `DelimitedFiles` package to parse CSV data directly, without having to do your own splitting and parsing:

```julia
$ echo "1, 2, 3, 4" | julia -e "using DelimitedFiles; @show readdlm(stdin, ',')"
readdlm(stdin, ',') = [1.0 2.0 3.0 4.0]

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 18, 2020, 10:14pm UTC](https://discourse.julialang.org/t/how-to-capture-standard-input-from-a-piped-command/39509/9 "2020-05-18T22:14:37Z")

</div>

You can also use `awk` for this kind of thing if you just want to stay in Unix-land.
