Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using <code>xxd</code>:</p> <pre><code>n=64 file=data tmp=tmp len=$(wc -c &lt; "$file") offset=$((len - n)) len=$((offset - n)) xxd -s -$n "$file" | xxd -r -s -$offset &gt; "$tmp" xxd -s $n -l $len "$file" | xxd -r &gt;&gt; "$tmp" xxd -l $n "$file" | xxd -r &gt;&gt; "$tmp" mv "$tmp" "$file" </code></pre> <p><strong>Edit:</strong></p> <p>Another approach would be to use <code>xxd</code> and <code>sed</code>:</p> <pre><code>n=64; hd=$((n * 2)) file=data tmp=tmp xxd -c $n -p "$file" | sed "1{x;d};:a;N;s/\n//;\${s/\(.*\)\(.\{$hd\}\)\$/\2\1/;G};ba" | xxd -r -p &gt; "$tmp" mv "$tmp" "$file" </code></pre> <p>Instead of six calls to <code>xxd</code>, it's two to <code>xxd</code> and one to <code>sed</code> (and fewer file reads and writes, too).</p> <p>Explanation of the <code>sed</code> command:</p> <ul> <li><code>1{x;d}</code> - Save the first line of hex digits in hold space. The length of the line is set to the number of bytes to swap using the <code>-c</code> option of <code>xxd</code>.</li> <li><code>:a</code> - Label "a" <ul> <li><code>N</code> - Append the next line.</li> <li><code>s/\n//</code> - Remove the embedded newline</li> <li><code>\${</code> - If it's the last line of input: <ul> <li><code>s/\(.*\)\(.\{$hd\}\)\$/\2\1/</code> - Swap the last <code>$hd</code> bytes to the beginning</li> <li><code>G</code> - Append the first <code>$n</code> bytes from hold space onto the end. Since it's the last line, the script ends.</li> </ul></li> <li><code>}</code> - end if</li> </ul></li> <li><code>ba</code> - Branch to label "a".</li> </ul> <p>Additionally, <code>sed</code> could do some manipulation of the data.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload