Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7429953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:03:51+00:00 2026-05-29T09:03:51+00:00

I have a (java) program that prints a line of hex numbers to stdout

  • 0

I have a (java) program that prints a line of hex numbers to stdout every 5ish seconds, until the program is terminated by the user.

I would like to redirect that output to a bash script so I could convert each of those hex numbers independently to decimal, then print the parsed line to stdout.

I tried using myProgram | myScript but that did the piping before any lines were printed, then didn’t keep listening to stdout. I then tried myProgram > myScript, and that just overwrote the script.

Ideas?

Edit: adding output from the runs, (sorry for the poor formatting, I couldn’t get it all in the code highlighting) so the middle of the output is not highighted).

Here is the script

#!/bin/bash
echo $0
echo $#
echo $1

Here is how my program runs while it goes straight to stdout this would continue forever if I didn’t terminate it.

mmmm@mmmm:~/mmmm/mmmm/mmmmm$ java net.tinyos.tools.Listen -comm 
serial@/dev/ttyUSB0:micaz
serial@/dev/ttyUSB0:57600: resynchronising   
00 FF FF 00 02 04 22 93 00 02 02 C9
00 FF FF 00 03 04 22 93 00 03 03 0E
00 FF FF 00 02 04 22 93 00 03 03 0E
00 FF FF 00 02 04 22 93 00 02 02 C9
^Z
[5]+  Stopped                 java net.tinyos.tools.Listen -comm          
serial@/dev/ttyUSB0:micaz

Here is where I try to pipe it to my script (which i have set to print the number of command line arguments and the first argument. It just freeze after this…

mmmm@mmmm:~/mmmm/mmmm/mmmmm$$ java net.tinyos.tools.Listen -comm serial@/dev/ttyUSB0:micaz | ./parser.sh
./parser.sh
0
serial@/dev/ttyUSB0:57600: resynchronising
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:03:53+00:00Added an answer on May 29, 2026 at 9:03 am

    Diagnosis

    When you use this script like this:

    java javaprog | myScript
    

    and myScript contains:

    #!/bin/bash
    echo $0
    echo $#
    echo $1
    

    Then the output from the script will be its name (myScript) from the echo $0, the number of arguments it was passed (0) from the echo $#, and the first argument (an empty line is echoed) from the echo $1. The script then exits (successfully). The issue is nothing to do with buffering; it is all to do with the script not reading anything from its standard input. Even a trivial modification would be an improvement:

    #!/bin/bash
    while read data; do echo $data; done
    

    That’s a slower form of cat, except that it normalizes random sequences of spaces and tabs into single spaces, stripping leading and trailing spaces off the line. It would at least demonstrate the script processing the output from the Java program.


    Trying awk

    To do what you’re after, you should probably replace that with an awk program or something similar. This is a first draft, but it stands some chance of working:

    awk '{for(i = 1; i <= NF; i++) { x = "0x" $i + 0; printf(" %d", x); printf "\n";}'
    

    This says ‘for each line (because there is no pattern before the open brace)’, do ‘for each of the fields 1..NF, convert the field into an explicit hex string with the 0x prefix and adding 0, then print the value as a decimal number (trusting awk to convert a string such as ‘0xC9’ to a number).

    Using Perl

    Unfortunately, a little testing shows that this does not work; the problem is getting a value other than 0 for x. So, … time to fall back on Perl in awk-emulation mode:

    $ echo '00 C9 28 13 A0 FF 01' |
    > perl -na -e 'for ($i = 0; $i < scalar(@F); $i++) { printf(" %d", hex $F[$i]); }
    >       printf "\n";'
     0 201 40 19 160 255 1
    $
    

    That works – it’s even fairly easy to understand. The -n option means ‘read each line of data and execute the commands in the script on each line (but do not print $_ at the end)’. The -a option combined with either -n (as here, or -p which is like -n except it prints $_ automatically) means ‘automatically split the input into the array @F. The script then processes each element of @F in each line (rather verbosely), using the hex function to convert the string in $F[$i] to a number and then printing that number with printf(). The verbosity can be reduced (this is Perl: There’s More Than One Way To Do It, or TMTOWTDI – tim-toady) with:

    $ echo '00 C9 28 13 A0 FF 01' |
    > perl -na -e 'foreach my $i (@F) { printf(" %d", hex $i); } printf "\n";'
     0 201 40 19 160 255 1
    $
    

    Same result, less code. There might be more abbreviated techniques; that’s compact enough without being wholly illegible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java program that executes from Spring Qquartz every 20 seconds. Sometimes
I have a Java program that creates a file and prints a bunch of
Problem: Write a program Deal.java that takes an command-line argument N and prints N
I have a Java program that loads thirdparty class files (classes I did not
We have a java program that requires a large amount of heap space -
I have a Java program that runs many small simulations. It runs a genetic
I have a java program that will work with a variety of Java Beans.
I have a Java program that is launched by a batch file with a
I have a Java program that generates Java classes for my application. Basically it
I have a Java program that opens a file using the RandomAccessFile class. I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.