views:

220

answers:

5

Hi All,

Is there a standard Linux command i can use to read a file chunk by chunk? For example, i have a file whose size is 6kB. I want to read/print the first 1kB, and then the 2nd 1kB ... Seems cat/head/tail wont work in this case.

Thanks very much.

A: 

split can split a file into pieces by given byte count

Laimoncijus
+5  A: 

You could do this with read -n in a loop:

while read -r -d '' -n 1024 BYTES; do
    echo "$BYTES"
    echo "---"
done < file.dat
John Kugelman
+3  A: 

dd will do it

dd if=your_file of=output_tmp_file bs=1024 count=1 skip=0

And then skip=1 for the second chunk, and so on.

You then just need to read the output_tmp_file to get the chunk.

Sorpigal
Thanks for the reply, it looks definitely promising. After i did:dd if=eco.log bs=1024 count=1 skip=1 (I prefer output to stdout)I got the information i want followed by:0+1 records in0+1 records out508 bytes (508 B) copied, 6.6e-05 seconds, 7.7 MB/sHow can i ignore above I/O stat info? I maned dd, and there isn't such silient option.
datasunny
Never mind, just found out those I/O stat are outputted to stderr. I can easily ignore those. Thanks all!
datasunny
A: 

you can use fmt

eg 10bytes

$ cat file
a quick brown fox jumps over the lazy dog
good lord , oh my gosh

$ tr '\n' ' '<file | fmt -w10 file
a quick
brown fox
jumps
over
the lazy
dog good
lord , oh
my gosh

each line is 10 characters. If you want to read the 2nd chunk, pass it to tools like awk ..eg

$ tr '\n' ' '<file |  fmt -w10 | awk 'NR==2' # print 2nd chunk
brown fox

To save each chunk to file, (or you can use split with -b )

$ tr '\n' ' '<file |  fmt -w10 | awk '{print $0 > "file_"NR}'
ghostdog74
A: 

Are you trying to actually read a text file? Like with your eyes? Try less or more

jdizzle