How can i write a program using STANDARD UNIX UTILITIES that will read data from standard input one character at a time and out the results to standard output. I know that it runs similar to what a C program in this case. I was told that this could be done in one line of code but never done Unix Pipeline Programming so i am just curious.
I think i might have it and would like any thoughts:
Your program simply does unbuffered reads (file descriptor 0) and unbuffered writes (file descriptor 1)
The pipelining cleverness is done by the shell and not your program.
main()
{
char buf[1000];
int n;
while ((n = read(0, buf, 1000) > 0)
write(1, buf, n);
}
Chris D
2012-12-03 13:59:48 UTC
I gave you a long detailed answer on this a few days ago when you asked it last time.
If you didn't like the answer then some constructive feedback would be helpful so that I can give you a better targeted answer.
In response to your attempt, this doesn't read "one character at a time"; it reads a stdio buffer's worth at a time. I assume you've also missed out some of the line, as you're definitively missing some instances of the pipe symbol.
(That's tr quote colon dash quote space quote space space quote, which translates colons and dashes (which would not normally be word separators to "wc") to white space.)
The tr command will read from its "standard input" and write to its "standard output", which becomes the input (pipe) to "wc" (word count). It will output white-space separated line and word counts (in that order). We pipe that to a sub-process that reads the counts and formats them as two right-justified, 8-character decimal fields.
starcher
2016-10-11 09:59:06 UTC
i imagine you advise "ls > similar.txt" use the completed command contained in the the double quote contained in the decision something you may do it in a unmarried step on the command instantaneous might want to nicely be used this way eg. device("ls -l | grep 'filename' | decrease -f1 > attempt.txt") or device("rsh -l username remotehost "kill pid"")
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.