Skip first 3 bytes of a file
Solution 1
Old school — you could use dd
:
dd if=A_FILE bs=1 skip=3
The input file is A_FILE
, the block size is 1 character (byte), skip the first 3 'blocks' (bytes). (With some variants of dd
such as GNU dd
, you could use bs=1c
here — and alternatives like bs=1k
to read in blocks of 1 kilobyte in other circumstances. The dd
on AIX does not support this, it seems; the BSD (macOS Sierra) variant doesn't support c
but does support k
, m
, g
, etc.)
There are other ways to achieve the same result, too:
sed '1s/^...//' A_FILE
This works if there are 3 or more characters on the first line.
tail -c +4 A_FILE
And you could use Perl, Python and so on too.
Solution 2
Instead of using cat
you can use tail
as such:
tail -c +4 FILE
This will print out the entire file except for the first 3 bytes. Consult man tail
for more information.
Related videos on Youtube
Manishkumar
Updated on September 18, 2022Comments
-
Manishkumar almost 2 years
I am using AIX 6.1 ksh shell.
I want to use one liner to do something like this:
cat A_FILE | skip-first-3-bytes-of-the-file
I want to skip the first 3 bytes of the first line; is there a way to do this?
-
squiguy over 11 years@BobDuell It's hard to post something that is compatible with every OS.
-
Manishkumar over 11 yearsYes, it works in AIX 6.1
-
Manishkumar over 11 yearsThanks for your help. Both the sed and the tail commands work in AIX 6.1. For the dd command, it should be
dd if=A_FILE bs=1 skip=3
in AIX 6.1 -
squiguy over 11 years@AlvinSIU Good to know. Glad I could help.
-
MUY Belgium over 10 yearsYou may want to use standard input as such cat A_FILE | tail -c +4 with gnu.
-
jimbobmcgee over 7 yearsIf only because I'm in that kind of mood, and don't like coding against the output of
ls
; have you considered usingstat -c'%s' "${IFILE}"
instead of thatls|awk
combo? That is, assuming GNU coreutils... -
Admin about 2 yearsThank you, this is a much better choice for working with large files with a tiny amount of garbage at the beginning. I used
dd
over an ssh connection to get a file image and I needed to remove the "[sudo] password for X:" at the beginning of the resulting file.