help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Use stdin without tempfile


From: Pierre Gaston
Subject: Re: [Help-bash] Use stdin without tempfile
Date: Mon, 2 Dec 2013 09:50:00 +0200

On Mon, Dec 2, 2013 at 12:59 AM, Peng Yu <address@hidden> 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.
>
> /tmp$ seq 20 > file.txt
> /tmp$ cat main.sh
> #!/usr/bin/env bash
>
> function prog1 {
> cat
> }
> function prog2 {
> cat
> }
> head -n 10 "$1" | prog1
> tail -n +11 "$1" | prog2
> /tmp$ ./main.sh file.txt
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> 11
> 12
> 13
> 14
> 15
> 16
> 17
> 18
> 19
> 20
>
> --
> Regards,
> Peng
>
>
you can perhaps use awk with process substitution like this:
printf %s\\n {1..30} | awk '{print > (NR<=10?p1:p2)}' p1=>(sed 's/^/first
/') p2=>(sed 's/^/second: /')

Or, if buffering is a problem:
printf %s\\n {1..30} | awk '{print > (NR<=10?p1:p2)}NR==10{fflush(0)}'
p1=>(sed 's/^/first /') p2=>(sed 's/^/second: /')
and if your awk doesn't have fflush:
printf %s\\n {1..30} | awk '{print > (NR<=10?p1:p2)}NR==10{system("")}'
p1=>(sed 's/^/first /') p2=>(sed 's/^/second: /')


Or the equivalent named pipe version:

mkfifo p1 p1;sed 's/^/first /'  <p1 & sed 's/^/second: /' <p2 & printf
%s\\n {1..30} | awk '{print > (NR<=10?"p1":"p2")}'

or with sed:
printf %s\\n {1..30} | sed -n  -e '1,10w '>(sed 's/^/first /') -e '11,$w
'>(sed 's/^/second: /' >&2)
mkfifo p1 p1;sed 's/^/first /'  <p1 & sed 's/^/second: /' <p2 & printf
%s\\n {1..30} | sed -n -e '1,10w p1' -e '11,$ w p2'


reply via email to

[Prev in Thread] Current Thread [Next in Thread]