Read stdin and pass through pipeline to tee

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I'm playing around with xinput to watch keyboard events.
I'd like to transform the output with awk and pass it to tee, printing to stdout and writing to a file simultaneously.



I watch events on device 14, type a single Space, then type Ctrl+C to exit:



$ xinput test 14
key release 36
key press 65
key release 65
key press 37
key press 54
^C


I can transform this with awk:



$ xinput test 14 | awk ' print $NF '
36
65
65
37
54
^C


Or I can pipe it to tee:



$ xinput test 14 | tee a.log
key release 36
key press 65
key release 65
key press 37
key press 54
^C

$ cat a.log
key release 36
key press 65
key release 65
key press 37
key press 54


But I can't pipe the transformed output to tee:



$ xinput test 14 | awk ' print $NF ' | tee b.log
^C

$ cat b.log



I think I understand what's going on, and can boil it down to a simpler example where I cat stdin to tr and pipe that to tee.



$ cat | tr a x | tee x.log


If I type A B Enter, then Ctrl+C, the pipeline aborts before printing to stdout or writing to x.log (though the file itself is created):



ab
^C

$ cat x.log


If I type Ctrl+D instead of Ctrl+C, an EOF character signals the end of input, and output gets printed to stdout and written to x.log:



ab
xb

$ cat x.log
xb



I've tried a couple different approaches, but so far no success.



$ awk ' print $NF ' ; > >(tee x.log)

$ exec fd> >(tee x.log)
$ xinput test 14 | awk ' print $NF ' >& $fd
$ exec fd>&-

$ exec > >(tee x.log)
$ xinput test 14 | awk ' print $NF '


Can this be done?







share|improve this question
























    up vote
    1
    down vote

    favorite












    I'm playing around with xinput to watch keyboard events.
    I'd like to transform the output with awk and pass it to tee, printing to stdout and writing to a file simultaneously.



    I watch events on device 14, type a single Space, then type Ctrl+C to exit:



    $ xinput test 14
    key release 36
    key press 65
    key release 65
    key press 37
    key press 54
    ^C


    I can transform this with awk:



    $ xinput test 14 | awk ' print $NF '
    36
    65
    65
    37
    54
    ^C


    Or I can pipe it to tee:



    $ xinput test 14 | tee a.log
    key release 36
    key press 65
    key release 65
    key press 37
    key press 54
    ^C

    $ cat a.log
    key release 36
    key press 65
    key release 65
    key press 37
    key press 54


    But I can't pipe the transformed output to tee:



    $ xinput test 14 | awk ' print $NF ' | tee b.log
    ^C

    $ cat b.log



    I think I understand what's going on, and can boil it down to a simpler example where I cat stdin to tr and pipe that to tee.



    $ cat | tr a x | tee x.log


    If I type A B Enter, then Ctrl+C, the pipeline aborts before printing to stdout or writing to x.log (though the file itself is created):



    ab
    ^C

    $ cat x.log


    If I type Ctrl+D instead of Ctrl+C, an EOF character signals the end of input, and output gets printed to stdout and written to x.log:



    ab
    xb

    $ cat x.log
    xb



    I've tried a couple different approaches, but so far no success.



    $ awk ' print $NF ' ; > >(tee x.log)

    $ exec fd> >(tee x.log)
    $ xinput test 14 | awk ' print $NF ' >& $fd
    $ exec fd>&-

    $ exec > >(tee x.log)
    $ xinput test 14 | awk ' print $NF '


    Can this be done?







    share|improve this question






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm playing around with xinput to watch keyboard events.
      I'd like to transform the output with awk and pass it to tee, printing to stdout and writing to a file simultaneously.



      I watch events on device 14, type a single Space, then type Ctrl+C to exit:



      $ xinput test 14
      key release 36
      key press 65
      key release 65
      key press 37
      key press 54
      ^C


      I can transform this with awk:



      $ xinput test 14 | awk ' print $NF '
      36
      65
      65
      37
      54
      ^C


      Or I can pipe it to tee:



      $ xinput test 14 | tee a.log
      key release 36
      key press 65
      key release 65
      key press 37
      key press 54
      ^C

      $ cat a.log
      key release 36
      key press 65
      key release 65
      key press 37
      key press 54


      But I can't pipe the transformed output to tee:



      $ xinput test 14 | awk ' print $NF ' | tee b.log
      ^C

      $ cat b.log



      I think I understand what's going on, and can boil it down to a simpler example where I cat stdin to tr and pipe that to tee.



      $ cat | tr a x | tee x.log


      If I type A B Enter, then Ctrl+C, the pipeline aborts before printing to stdout or writing to x.log (though the file itself is created):



      ab
      ^C

      $ cat x.log


      If I type Ctrl+D instead of Ctrl+C, an EOF character signals the end of input, and output gets printed to stdout and written to x.log:



      ab
      xb

      $ cat x.log
      xb



      I've tried a couple different approaches, but so far no success.



      $ awk ' print $NF ' ; > >(tee x.log)

      $ exec fd> >(tee x.log)
      $ xinput test 14 | awk ' print $NF ' >& $fd
      $ exec fd>&-

      $ exec > >(tee x.log)
      $ xinput test 14 | awk ' print $NF '


      Can this be done?







      share|improve this question












      I'm playing around with xinput to watch keyboard events.
      I'd like to transform the output with awk and pass it to tee, printing to stdout and writing to a file simultaneously.



      I watch events on device 14, type a single Space, then type Ctrl+C to exit:



      $ xinput test 14
      key release 36
      key press 65
      key release 65
      key press 37
      key press 54
      ^C


      I can transform this with awk:



      $ xinput test 14 | awk ' print $NF '
      36
      65
      65
      37
      54
      ^C


      Or I can pipe it to tee:



      $ xinput test 14 | tee a.log
      key release 36
      key press 65
      key release 65
      key press 37
      key press 54
      ^C

      $ cat a.log
      key release 36
      key press 65
      key release 65
      key press 37
      key press 54


      But I can't pipe the transformed output to tee:



      $ xinput test 14 | awk ' print $NF ' | tee b.log
      ^C

      $ cat b.log



      I think I understand what's going on, and can boil it down to a simpler example where I cat stdin to tr and pipe that to tee.



      $ cat | tr a x | tee x.log


      If I type A B Enter, then Ctrl+C, the pipeline aborts before printing to stdout or writing to x.log (though the file itself is created):



      ab
      ^C

      $ cat x.log


      If I type Ctrl+D instead of Ctrl+C, an EOF character signals the end of input, and output gets printed to stdout and written to x.log:



      ab
      xb

      $ cat x.log
      xb



      I've tried a couple different approaches, but so far no success.



      $ awk ' print $NF ' ; > >(tee x.log)

      $ exec fd> >(tee x.log)
      $ xinput test 14 | awk ' print $NF ' >& $fd
      $ exec fd>&-

      $ exec > >(tee x.log)
      $ xinput test 14 | awk ' print $NF '


      Can this be done?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 18 at 0:45









      ivan

      650718




      650718




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I found two solutions.




          1. Use stdbuf to run awk with no output buffering.



            xinput test 14 | stdbuf -o0 awk ' print $NF ' | tee b.log




          2. Add system("") call within the awk script



            xinput test 14 | awk ' print $NF; system("") ' | tee b.log







          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424880%2fread-stdin-and-pass-through-pipeline-to-tee%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I found two solutions.




            1. Use stdbuf to run awk with no output buffering.



              xinput test 14 | stdbuf -o0 awk ' print $NF ' | tee b.log




            2. Add system("") call within the awk script



              xinput test 14 | awk ' print $NF; system("") ' | tee b.log







            share|improve this answer


























              up vote
              0
              down vote













              I found two solutions.




              1. Use stdbuf to run awk with no output buffering.



                xinput test 14 | stdbuf -o0 awk ' print $NF ' | tee b.log




              2. Add system("") call within the awk script



                xinput test 14 | awk ' print $NF; system("") ' | tee b.log







              share|improve this answer
























                up vote
                0
                down vote










                up vote
                0
                down vote









                I found two solutions.




                1. Use stdbuf to run awk with no output buffering.



                  xinput test 14 | stdbuf -o0 awk ' print $NF ' | tee b.log




                2. Add system("") call within the awk script



                  xinput test 14 | awk ' print $NF; system("") ' | tee b.log







                share|improve this answer














                I found two solutions.




                1. Use stdbuf to run awk with no output buffering.



                  xinput test 14 | stdbuf -o0 awk ' print $NF ' | tee b.log




                2. Add system("") call within the awk script



                  xinput test 14 | awk ' print $NF; system("") ' | tee b.log








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 18 at 2:26

























                answered Feb 18 at 1:10









                ivan

                650718




                650718






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424880%2fread-stdin-and-pass-through-pipeline-to-tee%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay