Combine splitted low & high byte files into one file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine splitted low & high byte files into one file
# 1  
Old 10-31-2013
Combine splitted low & high byte files into one file

Hi all,

i have a binary file splitted into 2 chunks, first part with all high bytes and the second part with all low bytes.
I need to combine the two chunks into one binary file like (eg. exactly the reverse of the splitting method solved in the thread # 130940)

Code:
Hi bytes file content:
0000000     0   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30 
0000020    32  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62 
0000040  

Lo bytes file content: 
0000000     1   3   5   7   9  11  13  15  17  19  21  23  25  27  29  31 
0000020    33  35  37  39  41  43  45  47  49  51  53  55  57  59  61  63 
0000040

Expected result: 
0000000     0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15 
0000020    16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31 
0000040    32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47 
0000060    48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63 
0000100

Please help me to develop the combining script!

Thanks
mzs
# 2  
Old 10-31-2013
for gnu flavor awk, try:
Code:
awk '
NR==1 {f=NF}
{for (i=2; i<=NF; i++) a[c++]=$i}
END {
 asort(a);
 printf "%07d", d;
 for (i=1; i<=c; i++) {
    printf "%6d", a[i];
    if (!(i % (f-1))) {printf "\n%07d", d += 20}
 }
 if (!(i % (f-1))) {printf "\n%07d\n", (d += 20)} else {print ""};
}
' h_byte l_byte

# 3  
Old 10-31-2013
Try one more Awk version

Code:
$ cat file1
0000000     0   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30 
0000020    32  34  36  38  40  42  44  46  48  50  52  54  56  58  60  62 
0000040

Code:
$ cat file2
0000000     1   3   5   7   9  11  13  15  17  19  21  23  25  27  29  31 
0000020    33  35  37  39  41  43  45  47  49  51  53  55  57  59  61  63 
0000040

Code:
awk 'NR==FNR{
                  for(i=2;i<=NF;i++)
                  A[$1]=(i==2)?$i FS : A[$1] FS $i
                  next
            }

  ($1 in A){
            split(A[$1],Ar,FS)
        
            for(i=2;i<=NF;i++){
                                 printf (i==2)? !x ? sprintf("%07d",x) OFS Ar[i-1] OFS $i : \
                                 sprintf("%07d",x+=20) OFS Ar[i-1] OFS $i : \
                                 (i==10)? RS sprintf("%07d",x+=20) OFS Ar[i-1] OFS $i : \
                                 OFS Ar[i-1] OFS $i 
                              }
            printf RS
           }'   file1 file2 


OR


Code:
awk '{
        for(i=2;i<=NF;i++)
        A[++n]=$i
     }

  END{
           asort(A)
           for(i=1;i<=length(A);i++){
                                         LFS = (i%16==0) ? RS sprintf("%07d",x+=20) OFS: OFS 
                                         printf (i==1)? sprintf("%07d",x) LFS A[i] LFS : A[i] LFS
                                    }        
           
     }' file1 file2

Resulting
Code:
0000000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0000020 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0000040 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
0000060 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63


Last edited by Akshay Hegde; 10-31-2013 at 03:31 PM..
# 4  
Old 10-31-2013
I believe you both misunderstood. The task is to combine two binary files into a new binary file. The sample data in the OP are dumps of the actual data, not the actual data itself.

The od-like output that your solutions produce would require further processing to generate the desired binary output.

Regards,
Alister
# 5  
Old 10-31-2013
How about using perl:

Code:
#!/usr/bin/perl -w

open HI, $ARGV[0] || die $!;
binmode HI;
open LO, $ARGV[1] || die $!;
binmode LO;

my $hidata;
my $lowdata;

while (read(HI, $hidata, 1)) {

   read(LO, $lowdata, 1) || last;

   printf "%c%c", ord($hidata),ord($lowdata);
}
close HI;
close LO;

Eg:

Code:
$ printf "\x0\x2\x4\x6\x8\xA" > Hi
$ printf "\x1\x3\x5\x7\x9\xB" > Lo
$ ./ans.pl Hi Lo > result
$ od -t d1 result
0000000    0    1    2    3    4    5    6    7    8    9   10   11
0000014

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 10-31-2013
@ alister you might be right.. I am not sure.. I thought this could be text file.

-A@
# 7  
Old 10-31-2013
As a shell script you could try this:

Code:
#!/bin/bash

exec 3< $1
exec 4< $2

while IFS= read -N 1 -sru 3 Hi
do
   IFS= read -N 1 -sru 4 Lo || break
   [ ${#Hi} -eq 0 ] && printf "\x0" || printf "%s" "$Hi"
   [ ${#Lo} -eq 0 ] && printf "\x0" || printf "%s" "$Lo"
done

exec 3>&-
exec 4>&-

Eg:
Code:
$ ./ans.sh Hi Lo > result
$ od -t d1 result
0000000    0    1    2    3    4    5    6    7    8    9   10   11
0000014

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

High RAM usage, extremely low swapping

Hi team I have three physical servers running on Red Hat Enterprise Linux Server release 6.2 with the following memory conditions: # cat /proc/meminfo | grep -i mem MemTotal: 8062888 kB MemFree: 184540 kB Shmem: 516 kB and the following swap conditions: ... (6 Replies)
Discussion started by: hedkandi
6 Replies

2. Shell Programming and Scripting

splitting newfile.txt file and executing each splitted files

split -l $split_count newfile.txt for i in $split_files* do if test -s $workingdir/$split_files* then ./<$i.out> fi done ... (4 Replies)
Discussion started by: sanjay mn
4 Replies

3. AIX

High Runqueue (R) LOW CPU LOW I/O Low Network Low memory usage

Hello All I have a system running AIX 61 shared uncapped partition (with 11 physical processors, 24 Virtual 72GB of Memory) . The output from NMON, vmstat show a high run queue (60+) for continous periods of time intervals, but NO paging, relatively low I/o (6000) , CPU % is 40, Low network.... (9 Replies)
Discussion started by: IL-Malti
9 Replies

4. Shell Programming and Scripting

How to combine 2 files and output the unique & difference?

Hi Guys, I have two input files and I want to combine them and get the unique values and differences and put them into one file. See below desired output file. Inputfile1: 1111111 2222222 3333333 7860068 7860069 7860071 7860072 Inputfile2: 4444444 (4 Replies)
Discussion started by: pinpe
4 Replies

5. UNIX for Dummies Questions & Answers

Kernel/ user space and high/ low mem

Need some clarification on this.... 1. how are kernel/ user spaces and high/low memory related? 2. What do they all mean when i have the kernel command line as: "console=ttyS0,115200 root=/dev/sda2 rw mem=exactmap memmap=1M@0 memmap=96M@1M irqpoll" or 2. what do mem and memmap mean in... (3 Replies)
Discussion started by: dragonpoint
3 Replies

6. Shell Programming and Scripting

Split file into chunks of low & high byte

Hi guys, i have a question about spliting a binary file into 2 chunks. First chunk with all high bytes and the second one with all low bytes. What unix tools can i use? And how can this be performed? I looked in manpages of split and dd but this does not help. Thanks (2 Replies)
Discussion started by: basta
2 Replies

7. Shell Programming and Scripting

Picking high and low variables in a bash script - possible?

Is it possible to have a bash script pick the highest and lowest values of four variables? I've been googling for this but haven't come up with anything. I have a script that assigns variables ($c0, $c1, $c2, and $c3) based on the coretemps from grep/sed statements of sensors. I'd like to also... (5 Replies)
Discussion started by: graysky
5 Replies

8. Shell Programming and Scripting

low & high values

on the file Ftp'd from the mainframe ,do we have any UNIX command to replace mainframe low and values to space or null. i tried using tr and it doesn't work ... Thanks (1 Reply)
Discussion started by: rlmadhav
1 Replies

9. UNIX for Dummies Questions & Answers

malloc returning NULL if freemem high & swapmem low (MPRAS version 3.03 )

Hi All,:) In my application malloc is returning NULL even though there is sufficient amount of free memory available but the swap memory is low. Is this possible that, if free memory is high & swap memory is low, malloc will not be able to allocate memory & return NULL ? Few details: ... (4 Replies)
Discussion started by: Ritesh Kumar
4 Replies

10. Solaris

malloc returning NULL if freemem high & swapmem low

Hi All, In my application malloc is returning NULL even though there is sufficient amount of free memory is available but swap memory is low. Is this possible that, if free memory is high & swap memory is low, malloc will not be able to allocate memory & return NULL ?:) Kindly look into... (5 Replies)
Discussion started by: Ritesh Kumar
5 Replies
Login or Register to Ask a Question