0

anyone knows the algorithm which would take 1 text, 2nd text and would give minimum instructions how to build 2nd text when I already know the 1st?

e.g. output of "uptime":

16:18  up 1 day, 12:15, 4 users, load averages: 2.23 2.66 2.53

after a minute:

16:19  up 1 day, 12:16, 4 users, load averages: 2.23 2.66 2.53

The only change is in time in this case.

Output is acceptable in any format Following sample is just one of the ideas I have how to build up information which would be used to build 2nd line from 1st.


Sample output when comparing 1st call of uptime to 2nd:

4:1:9
21:1:6

4:1:9 (at position 4 - 1 character to removed, add "9")

21:1:6 (at position 21 - 1 character to removed, add "6")


Maybe I could just use some command of git but I'm looking for a light solution as possible, preferably without any dependencies which could run on any machine which is capable to run a shell script.


Update to have a better view what I'm trying to do with this.

This diff solution is needed to save bytes. Trafikito.com agent is running on the server and is sending the output of some commands like uptime free -m etc. to the API endpoint. This happens once per minute and it generates some outbound traffic for the server. I would like to reduce this footprint on the network by sending just a diff. While most of the time just a couple of numbers in all output are changed.

diff - this tool compares line-by-line, which doesn't make sense for commands as uptime where the output is just one line.

cmp - this tool compares byte-by-byte but the output of the command is big. If the byte is inserted at the beginning of the string, it will show the difference for all the coming bytes. It could just show to shift all the rest and insert new thing in place x.

2
  • 1
    If the text is in file, diff will do the job.
    – unxnut
    Aug 5, 2018 at 13:36
  • diff compares line by line and just shows that line is changed when the number in uptime output is changed. Aug 5, 2018 at 20:22

2 Answers 2

1

Did you consider cmp:

cmp -lb file[12]
 5  70 8     71 9
22  65 5     66 6
1
  • updated the question. cmp produces huge output if the byte is inserted somewhere in front of the text. Aug 5, 2018 at 20:58
1
$ diff -e uptime1 uptime2
1c
16:19  up 1 day, 12:16, 4 users, load averages: 2.23 2.66 2.53
.

diff -e outputs an ed script, suitable for transforming the first file into the second. It only adds, removes or changes complete lines.

Creating and applying the script:

$ diff -e uptime1 uptime2 >diff.ed

Later:

$ { cat diff.ed; echo ',p'; } | ed -s uptime1 >newfile

This would transform the contents of uptime1 into what was in uptime2. The echo ',p' is to get ed to write the resulting file to standard output (this is then saved in newfile).

Alternatively, any of the four types of output from diff (normal, unified, context, and ed) may be applied using the patch utility:

$ patch uptime1 <diff.ed
Hmm...  Looks like an ed script to me...
Patching file uptime1 using Plan A...
Hmm...  Ignoring the trailing garbage.
done

See the manuals for diff and patch on your system.

3
  • diff compares line by line and I'm looking for some tool to compare char by char while a lot of times I have only single line of text but there is a change. With uptime example, it may be single number in the line. Diff shows that line is changed and that's it. Aug 5, 2018 at 20:20
  • 1
    @Lukas You would have to write your own tool for that, along with the a patch equivalent. You may also try parsing the output of wdiff...
    – Kusalananda
    Aug 5, 2018 at 20:25
  • seems so, will wait some more time for any answers before doing anything. Updated the question to better describe what the goal is of this diff. Thank you for the contribution :) Aug 5, 2018 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .