Linux shell command to read / print a fragment of a file with a piece

Is there a standard Linux command that I can use to read a piece of file with a piece? For example, I have a 6 KB file. I want to read / print the first 1kB, and then the second 1kB ... It seems that the cat / head / tail will not work in this case.

Many thanks.

+3
source share
5 answers

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 fragment, etc.

You just need to read the output_tmp_file file to get the snippet.

+3
source

You can do this with help read -nin a loop:

while read -r -d '' -n 1024 BYTES; do
    echo "$BYTES"
    echo "---"
done < file.dat
+6
source

split

+1

Are you trying to read a text file? What about your eyes? Try lessormore

+1
source

you can use fmt

e.g. 10 bytes

$ 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 has 10 characters. If you want to read the second snippet, pass it on to tools like awk..eg

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

To save each fragment to a file (or you can use splitwith -b)

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

Source: https://habr.com/ru/post/1735643/


All Articles