Converting unknown binary data into series of numbers? (with a known example) - unix

I'm trying to find a way to convert files in a little-used archaic file format into something human readable...
As an example, od -x myfile gives:
0000000 2800 4620 1000 461e c800 461d a000 461e
0000020 8000 461e 2800 461e 5000 461f b800 461e
0000040 b800 461d 4000 461c a000 461e 3800 4620
0000060 f800 4621 7800 462a e000 4622 2800 463c
0000100 2000 464a 1000 4654 8c00 4693 5000 4661
0000120 7000 46ac 6c00 46d1 a400 4695 3c00 470a
0000140 b000 46ca 7400 46e9 c200 471b 9400 469e
0000160 9c00 4709 cc00 4719 4000 46b0 6400 46cc
...
which I know corresponds to these integers:
10250 10116 10098 10152 10144 10122 10196 10158
10094 10000 10152 10254 10366 10910 10424 12042
12936 13572 18886 14420 22072 ...
but I have no idea how to convert one to the other!!
Many many thanks to anyone who can help.
If possible, general tips for what to try/where to begin in this situation would also be appreciated.
Update: I put the full binary file online here http://pastebin.com/YL2ApExG and the numbers it corresponds to here http://pastebin.com/gXNntsaJ
In the hex dump, it seems to alternate between four digits, presumably they correspond to the numbers I want? separated either by 4600 or 4700. Unfortunately, I don't know where to go from here!
Someone else asked below: the binary file is a .dat file generated by an old spectroscopy program... it's 1336 bytes and corresponds to 334 integers, so it's four bytes per integer.

Well this is what you can do -
Step I: Do the od -x of the file and redirect it to a temp file (eg. hexdump.txt)
od -x myfile > hexdump.txt
Step II: You will now have a text file that contains hexadecimal values which you can view using the cat command. Something like this -
[jaypal~/Temp]$ cat hexdump.txt
0000000 2800 4620 1000 461e c800 461d a000 461e
0000020 8000 461e 2800 461e 5000 461f b800 461e
0000040 b800 461d 4000 461c a000 461e 3800 4620
0000060 f800 4621 7800 462a e000 4622 2800 463c
0000100 2000 464a 1000 4654 8c00 4693 5000 4661
0000120 7000 46ac 6c00 46d1 a400 4695 3c00 470a
0000140 b000 46ca 7400 46e9 c200 471b 9400 469e
0000160 9c00 4709 cc00 4719 4000 46b0 6400 46cc
Step III: The first column isn't really important to you. Columns 2 thru 9 are important. We will now strip the file using AWK so that you can convert it to decimal. We will add space so that we can consider each value as an individual field. We will also add "0x" to it so that we can pass it as a hexadecimal value.
[jaypal~/Temp]$ awk '{for (i=2;i<=NF;i++) printf "0x"$i" "}' hexdump.txt > hexdump1.txt
[jaypal~/Temp]$ cat hexdump1.txt
0x2800 0x4620 0x1000 0x461e 0xc800 0x461d 0xa000 0x461e 0x8000 0x461e 0x2800 0x461e 0x5000 0x461f 0xb800 0x461e 0xb800 0x461d 0x4000 0x461c 0xa000 0x461e 0x3800 0x4620 0xf800 0x4621 0x7800 0x462a 0xe000 0x4622 0x2800 0x463c 0x2000 0x464a 0x1000 0x4654 0x8c00 0x4693 0x5000 0x4661 0x7000 0x46ac 0x6c00 0x46d1 0xa400 0x4695 0x3c00 0x470a 0xb000 0x46ca 0x7400 0x46e9 0xc200 0x471b 0x9400 0x469e 0x9c00 0x4709 0xcc00 0x4719 0x4000 0x46b0 0x6400 0x46cc
Step IV: Now we will convert each hexadecimal value into decimal using printf function with AWK.
[jaypal~/Temp]$ gawk --non-decimal-data '{ for (i=1;i<=NF;i++) printf ("%05d ", $i)}' hexdump1.txt > hexdump2.txt
[jaypal~/Temp]$ cat hexdump2.txt
10240 17952 04096 17950 51200 17949 40960 17950 32768 17950 10240 17950 20480 17951 47104 17950 47104 17949 16384 17948 40960 17950 14336 17952 63488 17953 30720 17962 57344 17954 10240 17980 08192 17994 04096 18004 35840 18067 20480 18017 28672 18092 27648 18129 41984 18069 15360 18186 45056 18122 29696 18153 49664 18203 37888 18078 39936 18185 52224 18201 16384 18096 25600 18124
Step V: Formatting to make it easily readable
[jaypal~/Temp]$ sed 's/.\{48\}/&\n/g' < hexdump2.txt > hexdump3.txt
[jaypal~/Temp]$ cat hexdump3.txt
10240 17952 04096 17950 51200 17949 40960 17950
32768 17950 10240 17950 20480 17951 47104 17950
47104 17949 16384 17948 40960 17950 14336 17952
63488 17953 30720 17962 57344 17954 10240 17980
08192 17994 04096 18004 35840 18067 20480 18017
28672 18092 27648 18129 41984 18069 15360 18186
45056 18122 29696 18153 49664 18203 37888 18078
39936 18185 52224 18201 16384 18096 25600 18124

Related

R indexing and list translation

i have a list with indexes like this:
> mid_cp
[1] 3065 4871 13153 15587 18100 24010 26324 25648 38195 38196 39384 42237 45686 54217 55032 63684 62800 9134 35261 36449 36866 53968 16969
[24] 43529 46995 52351 4174 7011 18962 18151 18889 24036 32916 34061 34815 36866 51973 55802 53593 55421 56615 88 150 161 192 781
[47] 830 1300 1573 2396 2784 2547 3214 3135 3297 3301 4053 4249 4919 5856 6297 7328 7621 7708 8063 8219 8864 8887 9201
[70] 9214 9533 10334 10301 11235 10529 11356 10566 10872 12228 12250 12507 12048 12643 12913 13224 14297 16772 15363 18759 18979 16264 17363
[93] 20732 17971 22194 22422 19417 22903 22929 23087 19627 19961 23954 24297 25422 25423 25704 25765 25780 22769 22796 26871 27095 23789 24066
[116] 24069 27423 24366 24600 24871 25110 28374 26280 27873 29722 28839 29063 31031 31150 31546 32491 30356 33045 30863 33555 34201 34404 34684
[139] 35498 32912 33207 35874 33488 33716 36761 34543 36807 37000 35157 38195 38196 38458 36438 36619 39484 40109 37532 40143 40160 40458 41257
[162] 38434 38653 41866 41899 39429 42818 40001 43398 43441 40282 40566 43979 43996 40793 40806 40992 41065 41102 41330 41964 46322 43351 46670
and I have a table like this:
> head(movie.cp)
name id
252 $ (Dollars) (The Heist) 252
253 $5 a Day (Five Dollars a Day) 253
1 $9.99 1
254 $windle (Swindle) 254
255 "BBC2 Playhouse" Caught on a Train 255
256 "Independent Lens" Race to Execution 256
How do i get the mid_cp list to be a name list using the movie.cp table?
P.S.: I am completely newbie regarding R
are the numbers in mid_cp equivalent to movie.cp$id? if so try mid_cp <- movie.cp$name[match(mid_cp,movie.cp$id)]

Skip 2 lines after reading 1 line of a file using AWK

I have a huge file which i am reading through awk , using awk i am calculating the sum of values on that file.
Below is the file format i have :
18/11/13 00:00:50 585 17353 296883 666
18/11/13 00:01:50 965 26536 216201 558
18/11/13 00:02:50 990 38685 390537 768
18/11/13 00:03:50 1004 22435 377633 404
18/11/13 00:04:50 709 15754 161435 12062
18/11/13 00:05:50 96 7084 403551 0
18/11/13 00:06:50 107 14588 504683 597
18/11/13 00:07:50 115 27562 457555 814
awk '{sum+=$4; ++n} END {print " Tot="n," Avg="sum/n}' filename
Now i thin i want to skip 2 rows after we read a row from the file.
for your comment under your question, you just need to skip line2 and 3:
awk 'NR==1||NR>3{sum+=$4; ++n} END {print " Tot="n," Avg="sum/n}' filename

R: How to calculate accuracy in ETS of test set?

I am having a problem with the calculating of accuracy in ETS of test set.
train_ts<- ts(head(t$value,141), frequency=7) # this is train set (first 141 rows)
fit=auto.arima(train_ts)
forecasts = forecast(fit,h=12)
vector = ts(tail(t$value,12),frequency=7) # this is test set (last 12 rows)
accuracy(forecasts, vector, test=NULL, d=NULL, D=NULL) # I try to calculate accuracy
And I have this error:
Error in window.default(x, ...) : 'start' cannot be after 'end'
In addition: Warning message:
In window.default(x, ...) : 'start' value not changed
Result of forecasting:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
191 4742.038402 3781.130910 5702.945894 3272.457210 6211.619593
192 5068.467231 4105.169285 6031.765177 3595.230155 6541.704307
193 5233.951079 4270.487205 6197.414954 3760.460238 6707.441921
194 4883.850503 3910.172814 5857.528191 3394.738981 6372.962025
195 4857.666612 3883.140593 5832.192631 3367.257681 6348.075543
196 5180.408585 4203.616284 6157.200886 3686.533674 6674.283496
197 5091.348011 4112.687519 6070.008503 3594.615948 6588.080074
198 4833.290365 3848.222297 5818.358433 3326.758761 6339.821969
199 5003.034291 4017.771775 5988.296807 3496.205304 6509.863278
200 5175.020752 4189.555595 6160.485908 3667.881854 6682.159650
201 4963.008654 3972.665298 5953.352010 3448.409193 6477.608114
202 4882.858876 3890.856391 5874.861360 3365.721997 6399.995754
vector:
Time Series:
Start = 1
End = 12
Frequency = 1
[1] 5243 5010 5374 4952 6911 4260 6063 5597 4536 5522 4254 5048
How can I fix my error or how can I calculate accuracy correctly?
Example data (t$value):
[1] 5564 6657 7184 6456 5597 5951 6771 5990 6289 6885 6171 4739 5737 5950 6721
[16] 6579 6763 6829 5779 5346 5652 6319 6407 7232 6600 6244 5631 5198 6360 7922
[31] 6035 4221 4361 4475 5585 4845 5958 6833 3617 5036 4560 3820 5724 6352 5773
[46] 6200 4378 5614 5165 6345 5769 6228 6378 4827 4402 5829 4880 6333 6406 434
[61] 4754 4303 5498 5048 6042 6664 5492 5684 6194 5349 5846 5916 5069 5071 4367
[76] 5381 5694 5731 6029 5639 5539 4490 5223 5436 5819 941 6576 5235 3574 6319
[91] 5063 5765 5919 6006 5479 3653 4281 5433 4851 5543 5995 5049 4728 5449 5728
[106] 6009 5378 5730 5206 4764 5458 5970 5254 5653 5539 1907 4438 5421 5529 5225
[121] 6158 5572 4777 4575 5275 4742 5648 5198 5624 4781 3959 4368 5478 4681 5288
[136] 5758 4540 3899 5760 4797 5580 5433 4898 4473 3566 4779 4897 5099 5866 6231
[151] 4982 4375 5976
Firstly, something seems off in the forecast output you posted; it starts at point 191 which means the fitted series ended at 190, but that doesn't seem right given the code you posted.
Regardless, DatamineR is correct in his comment. You are providing two time series with different ranges of time. The forecast function will pick up where the fitted time series left off, but when you use ts(tail(t$value,12),frequency=7) you are creating a new time series that starts at 1.
One option is to convert one (or both) into numeric vectors, as DatamineR suggested. Otherwise you can set the start time for your test set to the correct value doing something like:
vector = ts(tail(t,12),start=end(train_ts)+c(0, 1), frequency=7)
where end(train_ts) gives you the last time point of the training series, and then I added one more time step (in the same cycle) by adding c(0,1) to set the start time of the test series.

How to retrieve values from a numeric matrix using the which() function in R?

I have a numeric matrix from which I want to retrieve the index given specific values.
I am trying the which() function to find values in the matrix.
The problem is that some values are found and some are not.
My matrix is as follows:
x_lat <- as.double(seq(48.0 ,60.0, by=0.1))
y_long <- as.double(seq(-10.0 ,2.0, by=0.1))
xv <- as.double(rep(x_lat,each = 121))
yv <- as.double(rep(y_long, 121))
vMatrix <- as.matrix(cbind(xv,yv))
If I want to retrieve the indices where the value -2.3 is TRUE the function returns correctly a vector with the indices where -2.3 appears.
xx<- which(vMatrix==-2.3,arr.ind=TRUE)
> xx
[1] 78 199 320 441 562 683 804 925 1046 1167 1288 1409 1530 1651 1772 1893 2014 2135 2256 2377 2498
[22] 2619 2740 2861 2982 3103 3224 3345 3466 3587 3708 3829 3950 4071 4192 4313 4434 4555 4676 4797 4918 5039
[43] 5160 5281 5402 5523 5644 5765 5886 6007 6128 6249 6370 6491 6612 6733 6854 6975 7096 7217 7338 7459 7580
[64] 7701 7822 7943 8064 8185 8306 8427 8548 8669 8790 8911 9032 9153 9274 9395 9516 9637 9758 9879 10000 10121
[85] 10242 10363 10484 10605 10726 10847 10968 11089 11210 11331 11452 11573 11694 11815 11936 12057 12178 12299 12420 12541 12662
[106] 12783 12904 13025 13146 13267 13388 13509 13630 13751 13872 13993 14114 14235 14356 14477 14598
But for some numbers (that appear in the matrix) the function does not work, e.g.,
xx<- which(vMatrix==-2.2,arr.ind=TRUE)
> xx
integer(0)
Floating point numbers can be misleading. Two such numbers are usually not "equal", even though the console may display the same output. The machine only has a certain accuracy with which it can represent the numbers.
Here's a simple example:
a <- 0.15 - 1/8
b <- 0.025
> a
[1] 0.025
> b
[1] 0.025
However, if we compare these numbers with "==", we obtain:
> a==b
[1] FALSE
That is because there are differences resulting from the floating point arithmetic which are beyond the machine's accuracy:
> a-b
[1] -6.938894e-18
Probably you can resolve the issue by simply rounding the numbers in the matrix to the necessary amount of relevant digits, like, e.g.,
xx<- which(round(vMatrix,3)==-2.2,arr.ind=TRUE)

What do values mean in inode column (proc/net/tcp(6))?

This is a piece of /proc/net/tcp file:
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
6: 1904A8C0:AC35 9603020A:1ED0 01 00000000:00000000 00:00000000 00000000 10055 0 8506 2 c1624900 129 0 0 10 -1
7: 1904A8C0:E8C4 13A11C1F:0050 06 00000000:00000000 03:00001390
00000000 0 0 0 3 d6267780
Can anyone explain me what do values mean in the inode column? I`m intrested in two cases.
8506 2 c1624900 129 0 0 10 -1
0 3 d6267780
I only know first value is inode number (unique file number).
linux-2.6.35.6/net/ipv4/tcp_ipv4.c
2358 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
2359 "%08X %5d %8d %lu %d %p %lu %lu %u %u %d%n",
2360 i, src, srcp, dest, destp, sk->sk_state,
2361 tp->write_seq - tp->snd_una,
2362 rx_queue,
2363 timer_active,
2364 jiffies_to_clock_t(timer_expires - jiffies),
2365 icsk->icsk_retransmits,
2366 sock_i_uid(sk),
2367 icsk->icsk_probes_out,
2368 sock_i_ino(sk),
2369 atomic_read(&sk->sk_refcnt), sk,
2370 jiffies_to_clock_t(icsk->icsk_rto),
2371 jiffies_to_clock_t(icsk->icsk_ack.ato),
2372 (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
2373 tp->snd_cwnd,
2374 tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh,
2375 len);

Resources