1753310 Members
6398 Online
108792 Solutions
New Discussion юеВ

read in file

 
Bhoyet Orillo
Occasional Contributor

read in file

Is there a way to read a specified number of bytes in
a file using a script?
5 REPLIES 5
Steven Sim Kok Leong
Honored Contributor

Re: read in file

Hi,

Please refer to:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xbe4ac4c76f92d611abdb0090277a778c,00.html

Hope this helps. Regards.

Steven Sim Kok Leong
Mark Greene_1
Honored Contributor

Re: read in file

see the man page for dd. There are flags to specify the source file and the block size in 512-byte increments.

HTH
mark
the future will be a lot like now, only later
Peter Kloetgen
Esteemed Contributor

Re: read in file

Hi Bhoyet,

you can use the dd- command:

dd if=/path/to/file bs=1 count=30

brings 30 bytes to standard output.

var=`dd if=/path/to/file bs=1 count=30`

gives the first 30 bytes of your file as value to the variable "var"

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Rodney Hills
Honored Contributor

Re: read in file

Use could us "head" command with "-c" option.

head -c -n 1000 filex >first1000bytes

Would copy only the first 1000 bytes from filex.

-- Rod Hills
There be dragons...
A. Clay Stephenson
Acclaimed Contributor

Re: read in file

Really Perl is the scripting language of choice for this.


open F1, "$n = read(F1,$s_in,8192);
# $n - number of bytes read
# $s_in contains the data up to 8192 bytes

# now seek to offset 32768 and read again
seek(F1,32768,0);
$n = read(f1,$s_in,8192);
close F1;

You would, of course, need to add some error checking but this is the clean scripting way to do this.
If it ain't broke, I can fix that.