Copy stdin to stdout: Difference between revisions
(C) |
|||
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|AWK}}== |
|||
Using the pattern // (which matches anything) with the default action (which is to print the current line) the following program will copy lines from stdin to stdut. |
|||
<lang AWK>//</lang> |
|||
=={{Header|C}}== |
=={{Header|C}}== |
Revision as of 15:43, 11 November 2018
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.
AWK
Using the pattern // (which matches anything) with the default action (which is to print the current line) the following program will copy lines from stdin to stdut. <lang AWK>//</lang>
C
<lang C>
- include <stdio.h>
int main(){
char c; while ( (c=getchar()) != EOF ){ putchar(c); } return 0;
} </lang>
Perl
<lang perl> perl -pe </lang>
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>
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>
sed
<lang sh> sed -e </lang>