parse output and check if a change

Discussion in 'Java' started by mike, Mar 7, 2013.

  1. mike

    mike Guest

    Hi,

    I am sending a command to unix and then I parse the output. The output is stored in a Info object ( Info.java). This will be used later in application..

    The problem is that the output from the unix command might change. So I want to make sure that if there is a change then we parse and update the Info object. But if there is no change then we do not need to parse and extract the information. We can use the Info object directly.

    The idea I have is to use the output from the command ( a text string) and calculate some kind of hash/md5sum.

    So the next time I run the unix command I take the output (a string) and calculate hash/md5 and see if it is an exact match as was generated the firsttime.

    Anyone will to give a hint on how to implememt this using java?

    br,

    //mike
    mike, Mar 7, 2013
    #1
    1. Advertising

  2. mike

    Arne Vajhøj Guest

    On 3/7/2013 2:10 PM, mike wrote:
    > I am sending a command to unix and then I parse the output. The
    > output is stored in a Info object ( Info.java). This will be used
    > later in application.
    >
    > The problem is that the output from the unix command might change. So
    > I want to make sure that if there is a change then we parse and
    > update the Info object. But if there is no change then we do not need
    > to parse and extract the information. We can use the Info object
    > directly.
    >
    > The idea I have is to use the output from the command ( a text
    > string) and calculate some kind of hash/md5sum.
    >
    > So the next time I run the unix command I take the output (a string)
    > and calculate hash/md5 and see if it is an exact match as was
    > generated the first time.
    >
    > Anyone will to give a hint on how to implememt this using java?


    You will need to code your logic, but hashing a String is easy:

    MessageDigest md = MessageDigest.getInstance("SHA-256");
    byte[] hash = md.digest(data.getBytes());

    or maybe:

    MessageDigest md = MessageDigest.getInstance("SHA-256");
    String hash = toHex(md.digest(data.getBytes()));

    ....

    private static String toHex(byte[] ba) {
    char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    StringBuffer sb = new StringBuffer("");
    for (int i = 0; i < ba.length; i++) {
    sb.append(hexdigit[(ba >> 4) & 0x0F]);
    sb.append(hexdigit[ba & 0x0F]);
    }
    return sb.toString();
    }

    Arne
    Arne Vajhøj, Mar 7, 2013
    #2
    1. Advertising

  3. mike

    Arne Vajhøj Guest

    On 3/7/2013 2:14 PM, Arne Vajhøj wrote:
    > On 3/7/2013 2:10 PM, mike wrote:
    >> I am sending a command to unix and then I parse the output. The
    >> output is stored in a Info object ( Info.java). This will be used
    >> later in application.
    >>
    >> The problem is that the output from the unix command might change. So
    >> I want to make sure that if there is a change then we parse and
    >> update the Info object. But if there is no change then we do not need
    >> to parse and extract the information. We can use the Info object
    >> directly.
    >>
    >> The idea I have is to use the output from the command ( a text
    >> string) and calculate some kind of hash/md5sum.
    >>
    >> So the next time I run the unix command I take the output (a string)
    >> and calculate hash/md5 and see if it is an exact match as was
    >> generated the first time.
    >>
    >> Anyone will to give a hint on how to implememt this using java?

    >
    > You will need to code your logic, but hashing a String is easy:
    >
    > MessageDigest md = MessageDigest.getInstance("SHA-256");
    > byte[] hash = md.digest(data.getBytes());
    >
    > or maybe:
    >
    > MessageDigest md = MessageDigest.getInstance("SHA-256");
    > String hash = toHex(md.digest(data.getBytes()));
    >
    > ...
    >
    > private static String toHex(byte[] ba) {
    > char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
    > '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    > StringBuffer sb = new StringBuffer("");
    > for (int i = 0; i < ba.length; i++) {
    > sb.append(hexdigit[(ba >> 4) & 0x0F]);
    > sb.append(hexdigit[ba & 0x0F]);
    > }
    > return sb.toString();
    > }


    MD5 is obsolete from a cryptographic point of view, so I
    switched to SHA-256.

    Arne
    Arne Vajhøj, Mar 7, 2013
    #3
  4. mike

    Joerg Meier Guest

    On Thu, 07 Mar 2013 14:14:20 -0500, Arne Vajhøj wrote:

    > private static String toHex(byte[] ba) {
    > char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
    > '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    > StringBuffer sb = new StringBuffer("");
    > for (int i = 0; i < ba.length; i++) {
    > sb.append(hexdigit[(ba >> 4) & 0x0F]);
    > sb.append(hexdigit[ba & 0x0F]);
    > }
    > return sb.toString();
    > }


    For future reference:

    DatatypeConverter.printHexBinary(ba).toLowerCase();

    Liebe Gruesse,
    Joerg

    --
    Ich lese meine Emails nicht, replies to Email bleiben also leider
    ungelesen.
    Joerg Meier, Mar 7, 2013
    #4
  5. mike

    Arne Vajhøj Guest

    On 3/7/2013 5:50 PM, Joerg Meier wrote:
    > On Thu, 07 Mar 2013 14:14:20 -0500, Arne Vajhøj wrote:
    >
    >> private static String toHex(byte[] ba) {
    >> char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
    >> '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    >> StringBuffer sb = new StringBuffer("");
    >> for (int i = 0; i < ba.length; i++) {
    >> sb.append(hexdigit[(ba >> 4) & 0x0F]);
    >> sb.append(hexdigit[ba & 0x0F]);
    >> }
    >> return sb.toString();
    >> }

    >
    > For future reference:
    >
    > DatatypeConverter.printHexBinary(ba).toLowerCase();


    Ah - my little toHex is no longer needed.

    Thanks.

    Arne
    Arne Vajhøj, Mar 8, 2013
    #5
  6. mike

    Daniel Pitts Guest

    On 3/7/13 11:10 AM, mike wrote:
    > Hi,
    >
    > I am sending a command to unix and then I parse the output. The output is stored in a Info object ( Info.java). This will be used later in application.
    >
    > The problem is that the output from the unix command might change. So I want to make sure that if there is a change then we parse and update the Info object. But if there is no change then we do not need to parse and extract the information. We can use the Info object directly.
    >
    > The idea I have is to use the output from the command ( a text string) and calculate some kind of hash/md5sum.
    >
    > So the next time I run the unix command I take the output (a string) and calculate hash/md5 and see if it is an exact match as was generated the first time.
    >
    > Anyone will to give a hint on how to implememt this using java?
    >
    > br,
    >
    > //mike
    >

    So, you're going to run a process every time (which is expensive), and
    then avoid parsing the result (which is likely far less expensive) if
    the output doesn't change?

    Is there perhaps a better way to get this Info that doesn't involve
    running an external program?
    Daniel Pitts, Mar 8, 2013
    #6
  7. In article <5138e77a$0$32107$>,
    Arne Vajhøj <> wrote:

    > On 3/7/2013 2:14 PM, Arne Vajhøj wrote:
    > > On 3/7/2013 2:10 PM, mike wrote:
    > >> I am sending a command to unix and then I parse the output. The
    > >> output is stored in a Info object ( Info.java). This will be used
    > >> later in application.
    > >>
    > >> The problem is that the output from the unix command might change. So
    > >> I want to make sure that if there is a change then we parse and
    > >> update the Info object. But if there is no change then we do not need
    > >> to parse and extract the information. We can use the Info object
    > >> directly.
    > >>
    > >> The idea I have is to use the output from the command ( a text
    > >> string) and calculate some kind of hash/md5sum.
    > >>
    > >> So the next time I run the unix command I take the output (a string)
    > >> and calculate hash/md5 and see if it is an exact match as was
    > >> generated the first time.
    > >>
    > >> Anyone will to give a hint on how to implememt this using java?

    > >
    > > You will need to code your logic, but hashing a String is easy:
    > >
    > > MessageDigest md = MessageDigest.getInstance("SHA-256");
    > > byte[] hash = md.digest(data.getBytes());
    > >
    > > or maybe:
    > >
    > > MessageDigest md = MessageDigest.getInstance("SHA-256");
    > > String hash = toHex(md.digest(data.getBytes()));
    > >
    > > ...
    > >
    > > private static String toHex(byte[] ba) {
    > > char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
    > > '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    > > StringBuffer sb = new StringBuffer("");
    > > for (int i = 0; i < ba.length; i++) {
    > > sb.append(hexdigit[(ba >> 4) & 0x0F]);
    > > sb.append(hexdigit[ba & 0x0F]);
    > > }
    > > return sb.toString();
    > > }

    >
    > MD5 is obsolete from a cryptographic point of view, so I
    > switched to SHA-256.
    >
    > Arne


    I would have just saved the original String if it's not too big.
    --
    I will not see posts from Google because I must filter them as spam
    Kevin McMurtrie, Mar 9, 2013
    #7
  8. mike

    Arne Vajhøj Guest

    On 3/8/2013 10:16 PM, Kevin McMurtrie wrote:
    > In article <5138e77a$0$32107$>,
    > Arne Vajhøj <> wrote:
    >
    >> On 3/7/2013 2:14 PM, Arne Vajhøj wrote:
    >>> On 3/7/2013 2:10 PM, mike wrote:
    >>>> I am sending a command to unix and then I parse the output. The
    >>>> output is stored in a Info object ( Info.java). This will be used
    >>>> later in application.
    >>>>
    >>>> The problem is that the output from the unix command might change. So
    >>>> I want to make sure that if there is a change then we parse and
    >>>> update the Info object. But if there is no change then we do not need
    >>>> to parse and extract the information. We can use the Info object
    >>>> directly.
    >>>>
    >>>> The idea I have is to use the output from the command ( a text
    >>>> string) and calculate some kind of hash/md5sum.
    >>>>
    >>>> So the next time I run the unix command I take the output (a string)
    >>>> and calculate hash/md5 and see if it is an exact match as was
    >>>> generated the first time.
    >>>>
    >>>> Anyone will to give a hint on how to implememt this using java?
    >>>
    >>> You will need to code your logic, but hashing a String is easy:
    >>>
    >>> MessageDigest md = MessageDigest.getInstance("SHA-256");
    >>> byte[] hash = md.digest(data.getBytes());
    >>>
    >>> or maybe:
    >>>
    >>> MessageDigest md = MessageDigest.getInstance("SHA-256");
    >>> String hash = toHex(md.digest(data.getBytes()));
    >>>
    >>> ...
    >>>
    >>> private static String toHex(byte[] ba) {
    >>> char hexdigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
    >>> '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    >>> StringBuffer sb = new StringBuffer("");
    >>> for (int i = 0; i < ba.length; i++) {
    >>> sb.append(hexdigit[(ba >> 4) & 0x0F]);
    >>> sb.append(hexdigit[ba & 0x0F]);
    >>> }
    >>> return sb.toString();
    >>> }

    >>
    >> MD5 is obsolete from a cryptographic point of view, so I
    >> switched to SHA-256.

    >
    > I would have just saved the original String if it's not too big.


    That is the simple solution.

    :)

    Arne
    Arne Vajhøj, Mar 9, 2013
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John M. Lembo

    Parse Text File and Output to File

    John M. Lembo, Aug 1, 2003, in forum: Perl
    Replies:
    0
    Views:
    12,694
    John M. Lembo
    Aug 1, 2003
  2. chuck amadi
    Replies:
    11
    Views:
    654
    Cameron Laird
    Jun 16, 2004
  3. chuck amadi
    Replies:
    1
    Views:
    477
    Larry Bates
    Jun 23, 2004
  4. Replies:
    19
    Views:
    1,104
    Daniel Vallstrom
    Mar 15, 2005
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    173
    7stud --
    Feb 20, 2008
Loading...

Share This Page