[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Use stdin without tempfile
From: |
Peng Yu |
Subject: |
Re: [Help-bash] Use stdin without tempfile |
Date: |
Sun, 1 Dec 2013 20:33:10 -0600 |
On Sun, Dec 1, 2013 at 5:51 PM, Matthew Cengia <address@hidden> wrote:
> On 2013-12-01 16:59, Peng Yu wrote:
>> Hi,
>>
>> I want to process stdin in the way that the first 10 lines are
>> processed by prog1, and the remaining lines are processed by prog2.
>>
>> The output of prog1 is printed to stdout first, then the output of
>> prog2 is printed to stdout. No tempfiles should be created, but fifo
>> can be created. Is it possible?
>>
>> When the input is not stdin, a script like the following can be easily
>> created. But when the input is a pipe, it is not clear to me what to
>> do without creating a tempfile.
>
> bash$ printf "%s\n" {1..30} > file.txt
> bash$ cat main.sh
> #!/bin/bash
>
> prog1(){
> local line
> while read -r line
> do
> printf "PROG1: %s\n" "$line"
> done
> }
> prog2(){
> local line
> while read -r line
> do
> printf "PROG2: %s\n" "$line"
> done
> }
>
> {
> for i in {1..10}
> do read -r line
> echo "$line"
> done | prog1
>
> prog2
> } < "$1"
10 is just an example. When 10 becomes a very large number or the
input is very long, I guess this is not very efficient? Is there a
more efficient solution?
> bash$ ./main.sh file.txt
> PROG1: 1
> PROG1: 2
> PROG1: 3
> PROG1: 4
> PROG1: 5
> PROG1: 6
> PROG1: 7
> PROG1: 8
> PROG1: 9
> PROG1: 10
> PROG2: 11
> PROG2: 12
> PROG2: 13
> PROG2: 14
> PROG2: 15
> PROG2: 16
> PROG2: 17
> PROG2: 18
> PROG2: 19
> PROG2: 20
> PROG2: 21
> PROG2: 22
> PROG2: 23
> PROG2: 24
> PROG2: 25
> PROG2: 26
> PROG2: 27
> PROG2: 28
> PROG2: 29
> PROG2: 30
>
> --
> Regards,
> Matthew Cengia
--
Regards,
Peng