Copy stdin to stdout: Difference between revisions

From Rosetta Code
Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 2: Line 2:


Create an executable file that copies stdin to stdout, or else a script that does so through the invocation of an interpreter at the command line.
Create an executable file that copies stdin to stdout, or else a script that does so through the invocation of an interpreter at the command line.



=={{header|Ada}}==
=={{header|Ada}}==
Line 17: Line 16:
end loop;
end loop;
end Copy_Stdin_To_Stdout;</lang>
end Copy_Stdin_To_Stdout;</lang>



=={{header|Aime}}==
=={{header|Aime}}==
Line 38: Line 36:
END</lang>
END</lang>


=={{Header|AWK}}==
=={{header|AWK}}==
Using the awk interpreter, the following command uses the pattern // (which matches anything) with the default action (which is to print the current line) and so copy lines from stdin to stdut.
Using the awk interpreter, the following command uses the pattern // (which matches anything) with the default action (which is to print the current line) and so copy lines from stdin to stdut.
<lang AWK>awk "//"</lang>
<lang AWK>awk "//"</lang>


=={{Header|C}}==
=={{header|C}}==
<lang C>
<lang C>
#include <stdio.h>
#include <stdio.h>
Line 100: Line 98:
}
}
}</lang>
}</lang>

=={{Header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>main = interact id </lang>
<lang Haskell>main = interact id </lang>


Line 131: Line 130:
</pre>
</pre>


=={{Header|Julia}}==
=={{header|Julia}}==
<lang Julia>while !eof(stdin)
<lang Julia>while !eof(stdin)
write(stdout, read(stdin, UInt8))
write(stdout, read(stdin, UInt8))
Line 148: Line 147:
<pre>lua -e 'for x in io.lines() do print(x) end'</pre>
<pre>lua -e 'for x in io.lines() do print(x) end'</pre>


=={{Header|Mercury}}==
=={{header|Mercury}}==
<lang Mercury>
<lang Mercury>


Line 194: Line 193:
</lang>
</lang>


=={{Header|Nim}}==
=={{header|Nim}}==


<lang nim>stdout.write readAll(stdin)</lang>
<lang nim>stdout.write readAll(stdin)</lang>


=={{Header|OCaml}}==
=={{header|OCaml}}==


<lang ocaml>try
<lang ocaml>try
Line 206: Line 205:
with End_of_file -> ()</lang>
with End_of_file -> ()</lang>


=={{header|Perl}}==

=={{Header|Perl}}==
<lang perl>
<lang perl>
perl -pe ''
perl -pe ''
</lang>
</lang>

=={{header|Perl 6}}==
When invoked at a command line: Slightly less magical than Perl / sed. The p flag means automatically print each line of output to STDOUT. The e flag means execute what follows inside quotes. ".lines" reads lines from the assigned pipe (file handle), STDIN by default.

<lang perl6>perl6 -pe'.lines'</lang>

When invoked from a file: Lines are auto-chomped, so need to re-add newlines (hence .say rather than .print)
<lang perl6>.say for lines</lang>


=={{header|Phix}}==
=={{header|Phix}}==
Line 230: Line 220:
<lang PicoLisp>(in NIL (echo))</lang>
<lang PicoLisp>(in NIL (echo))</lang>


=={{Header|Prolog}}==
=={{header|Prolog}}==
<lang Prolog>
<lang Prolog>
%File: stdin_to_stdout.pl
%File: stdin_to_stdout.pl
Line 253: Line 243:
<pre>Rscript -e 'cat(readLines(file("stdin")))'</pre>
<pre>Rscript -e 'cat(readLines(file("stdin")))'</pre>


=={{Header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket
<lang racket>#lang racket


Line 262: Line 252:
(loop)]))</lang>
(loop)]))</lang>


=={{Header|REXX}}==
=={{header|Raku}}==
(formerly Perl 6)
When invoked at a command line: Slightly less magical than Perl / sed. The p flag means automatically print each line of output to STDOUT. The e flag means execute what follows inside quotes. ".lines" reads lines from the assigned pipe (file handle), STDIN by default.

<lang perl6>perl6 -pe'.lines'</lang>

When invoked from a file: Lines are auto-chomped, so need to re-add newlines (hence .say rather than .print)
<lang perl6>.say for lines</lang>

=={{header|REXX}}==
In the REXX language, &nbsp; the &nbsp; '''STDIN''' &nbsp; (default input
In the REXX language, &nbsp; the &nbsp; '''STDIN''' &nbsp; (default input
stream) &nbsp; is normally the console, &nbsp; and
stream) &nbsp; is normally the console, &nbsp; and
Line 275: Line 274:
end /*while*/ /*stick a fork in it, we're all done. */</lang>
end /*while*/ /*stick a fork in it, we're all done. */</lang>


=={{Header|Rust}}==
=={{header|Rust}}==
<lang Rust>use std::io;
<lang Rust>use std::io;


Line 282: Line 281:
}</lang>
}</lang>


=={{Header|Scheme}}==
=={{header|Scheme}}==


<lang scheme>
<lang scheme>
Line 290: Line 289:
</lang>
</lang>


=={{Header|sed}}==
=={{header|sed}}==


<lang sh>
<lang sh>

Revision as of 23:49, 13 March 2020

Copy stdin to stdout is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Create an executable file that copies stdin to stdout, or else a script that does so through the invocation of an interpreter at the command line.

Ada

<lang Ada>with Ada.Text_IO;

procedure Copy_Stdin_To_Stdout is

  use Ada.Text_IO;
  C : Character;

begin

  while not End_Of_File loop
     Get_Immediate (C);
     Put (C);
  end loop;

end Copy_Stdin_To_Stdout;</lang>

Aime

<lang aime>file f; data b; f.stdin; while (f.b_line(b) ^ -1) {

   o_(b, "\n");

}</lang>

ALGOL 68

Works with: ALGOL 68G version Any - tested with release 2.8.3.win32

<lang algol68>BEGIN

   BOOL at eof := FALSE;
   # set the EOF handler for stand in to a procedure that sets "at eof" to true #
   # and returns true so processing can continue                                #                               
   on logical file end( stand in, ( REF FILE f )BOOL: at eof := TRUE );
   # copy stand in to stand out                                                 #
   WHILE STRING line; read( ( line, newline ) ); NOT at eof DO write( ( line, newline ) ) OD

END</lang>

AWK

Using the awk interpreter, the following command uses the pattern // (which matches anything) with the default action (which is to print the current line) and so copy lines from stdin to stdut. <lang AWK>awk "//"</lang>

C

<lang C>

  1. include <stdio.h>

int main(){

 char c;
 while ( (c=getchar()) != EOF ){
   putchar(c);
 }
 return 0;

} </lang>

C++

<lang cpp>#include <iostream>

  1. include <iterator>

int main() {

   using namespace std;
   noskipws(cin);
   copy(
       istream_iterator<char>(cin),
       istream_iterator<char>(),
       ostream_iterator<char>(cout)
   );
   return 0;

}</lang>

D

<lang d>import std.stdio;

void main() {

   foreach (line; stdin.byLine) {
       writeln(line);
   }

}</lang>

Go

<lang go>package main

import (

   "bufio"
   "io"
   "os"

)

func main() {

   r := bufio.NewReader(os.Stdin)
   w := bufio.NewWriter(os.Stdout)
   for {
       b, err := r.ReadByte()       
       if err == io.EOF {
           return
       }
       w.WriteByte(b)
       w.Flush()
   }   

}</lang>

Haskell

<lang Haskell>main = interact id </lang>

Java

Copies until no more input. <lang java> import java.util.Scanner;

public class CopyStdinToStdout {

   public static void main(String[] args) {
       try (Scanner scanner = new Scanner(System.in);) {
           String s;
           while ( (s = scanner.nextLine()).compareTo("") != 0 ) {
               System.out.println(s);
           }
       }
   }

} </lang>

Output:

Output interleaved. Stdin and Stdout are same window.

Line 1.
Line 1.
Line 2.
Line 2.

Julia

<lang Julia>while !eof(stdin)

   write(stdout, read(stdin, UInt8))

end</lang>

Kotlin

<lang scala>fun main() {

   var c: Int
   do {
       c = System.`in`.read()
       System.out.write(c)
   } while (c >= 0)

}</lang>

Lua

lua -e 'for x in io.lines() do print(x) end'

Mercury

<lang Mercury>

- module stdin_to_stdout.
- interface.
- import_module io.
- pred main(io::di, io::uo) is det.

%-----------------------------------------------------------------------------% %-----------------------------------------------------------------------------%

- implementation.
- import_module char.
- import_module list.
- import_module string.

%-----------------------------------------------------------------------------%


main(!IO) :-

   io.read_line_as_string(Result, !IO),
   (
       Result = ok(Line),
       io.write_string(Line, !IO),
       main(!IO)
   ;
       Result = eof
   ;
       Result = error(Error),
       io.error_message(Error, Message),
       io.input_stream_name(StreamName, !IO),
       io.progname("stdin_to_stdout", ProgName, !IO),
       io.write_strings([
           ProgName, ": ",
           "error reading from `", StreamName, "': \n\t",
           Message, "\n"
       ], !IO)
   ).

%-----------------------------------------------------------------------------% </lang>

Nim

<lang nim>stdout.write readAll(stdin)</lang>

OCaml

<lang ocaml>try

 while true do
   output_char stdout (input_char stdin)
 done

with End_of_file -> ()</lang>

Perl

<lang perl> perl -pe </lang>

Phix

<lang Phix>while true do

   integer ch = wait_key()
   if ch=#1B then exit end if
   puts(1,ch)

end while</lang>

PicoLisp

<lang PicoLisp>(in NIL (echo))</lang>

Prolog

<lang Prolog> %File: stdin_to_stdout.pl

- initialization(main).

main :- repeat, get_char(X), put_char(X), X == end_of_file, fail. </lang>

Invocation at the command line (with Swi-prolog): <lang sh> swipl stdin_to_stdout.pl </lang>

Python

python -c 'import sys; sys.stdout.write(sys.stdin.read())'

R

Rscript -e 'cat(readLines(file("stdin")))'

Racket

<lang racket>#lang racket

(let loop ()

 (match (read-char)
   [(? eof-object?) (void)]
   [c (display c)
      (loop)]))</lang>

Raku

(formerly Perl 6) When invoked at a command line: Slightly less magical than Perl / sed. The p flag means automatically print each line of output to STDOUT. The e flag means execute what follows inside quotes. ".lines" reads lines from the assigned pipe (file handle), STDIN by default.

<lang perl6>perl6 -pe'.lines'</lang>

When invoked from a file: Lines are auto-chomped, so need to re-add newlines (hence .say rather than .print) <lang perl6>.say for lines</lang>

REXX

In the REXX language,   the   STDIN   (default input stream)   is normally the console,   and the   STDOUT   (default output stream)   is normally the console.   So for REXX, this task equates to copying data from the console to itself. <lang rexx>/*REXX pgm copies data from STDIN──►STDOUT (default input stream──►default output stream*/

 do while chars()\==0                           /*repeat loop until no more characters.*/
 call charin  , x                               /*read  a char from the  input stream. */
 call charout , x                               /*write "   "    "   "   output   "    */
 end   /*while*/                                /*stick a fork in it,  we're all done. */</lang>

Rust

<lang Rust>use std::io;

fn main() {

   io::copy(&mut io::stdin().lock(), &mut io::stdout().lock());

}</lang>

Scheme

<lang scheme> (do ((c (read-char) (read-char)))

   ((eof-object? c) 'done)
 (display c))

</lang>

sed

<lang sh> sed -e </lang>

zkl

<lang zkl>zkl --eval "File.stdout.write(File.stdin.read())"</lang>