Search code examples
bashcut

Cut -b does not stop at expected point


I'm trying to extract a range of bytes from a file. File contains continuous 16 bit sample data. I would think cut -b should work but I am getting error in the data.

Extracting 20 bytes (10 samples)

cut -b188231561-188231580 file.dat > out.dat

I expect it to create a 20 byte file with 10 samples (last sample should be the -79). However it creates a 5749bytes file, with the following contents (displayed using od -s)

0000000    -69    -87    -75    -68    -83    -94    -68    -67
0000020    -82    -79   2570   2570   2570   2570   2570   2570
0000040   2570   2570   2570   2570   2570   2570   2570   2570
*
0013140    -65    -67    -69    -69    -71    -66    -72    -68
0013160    -69    -80     10
0013165

As you can see, there is a whole bunch of repeated 2570 values where cut was supposed to stop.

What am I doing wrong here? Also the Wikipedia article on cut says -b is limited to 1023 byte lines. Although the man pages for cut don't seem to mention this limitation.

Is there a better bash command to extract bytes N-M from a binary file? I already wrote a Perl script to do it. I'm just curious.


Solution

  • cut -b is used to get certain bytes from each line, it can't be used to get bytes from the file as a whole.

    You can use head/tail instead:

    N=120
    M=143
    
    tail -c +$N file | head -c $((M-N))