There is no easy way to do in-place editing using UNIX utilities, but there is one solution for modifying in-place files that you can modify to work for you (courtesy of Robert Bonomy at https://groups.google.com/forum/ #! topic / comp.unix.shell / 5PRRZIP0v64 ):
bytes=$(head -37 "$file" |wc -c) dd if="$file" bs="$bytes" skip=1 conv=notrunc of="$file"
The final file should be $count bytes less than the original (since the goal was to remove the $count bytes from the beginning), so to finish we must delete the last bytes of $count . We use conv=notrunc above to make sure that the file is not completely cleaned, but only truncated (see, for example, below). On a GNU system such as Linux, subsequent truncation can be done with:
truncate -s "-$bytes" "$file"
For example, to remove the first 5 lines from this file of 12 lines
$ wc -l file 12 file $ cat file When chapman billies leave the street, And drouthy neibors, neibors, meet; As market days are wearing late, And folk begin to tak the gate, While we sit bousing at the nappy, An' getting fou and unco happy, We think na on the lang Scots miles, The mosses, waters, slaps and stiles, That lie between us and our hame, Where sits our sulky, sullen dame, Gathering her brows like gathering storm, Nursing her wrath to keep it warm.
First, use dd to remove the target 5 lines (actually the $ bytes bytes) from the beginning of the file and copy the rest from the end to the foreground, but leave the ending bytes of $ bytes as they are:
$ bytes=$(head -5 file |wc -c) $ dd if=file bs="$bytes" skip=1 conv=notrunc of=file 1+1 records in 1+1 records out 253 bytes copied, 0.0038458 s, 65.8 kB/s $ wc -l file 12 file $ cat file An' getting fou and unco happy, We think na on the lang Scots miles, The mosses, waters, slaps and stiles, That lie between us and our hame, Where sits our sulky, sullen dame, Gathering her brows like gathering storm, Nursing her wrath to keep it warm. s, waters, slaps and stiles, That lie between us and our hame, Where sits our sulky, sullen dame, Gathering her brows like gathering storm, Nursing her wrath to keep it warm.
and then use truncate to remove these remaining bytes from the end:
$ truncate -s "-$bytes" "file" $ wc -l file 7 file $ cat file An' getting fou and unco happy, We think na on the lang Scots miles, The mosses, waters, slaps and stiles, That lie between us and our hame, Where sits our sulky, sullen dame, Gathering her brows like gathering storm, Nursing her wrath to keep it warm.
If we tried the above without dd... conv=notrunc :
$ wc -l file 12 file $ bytes=$(head -5 file |wc -c) $ dd if=file bs="$bytes" skip=1 of=file dd: file: cannot skip to specified offset 0+0 records in 0+0 records out 0 bytes copied, 0.0042254 s, 0.0 kB/s $ wc -l file 0 file
Check out the Google Groups thread I referenced for other suggestions and information.