Luca Lanziani

Thu 18 October 2018


Read file in chunks in bash

How to read a file in chunks. You can optionally specify the starting point and the chunk size.


function read_file_in_chunks { # FILENAME [START_FROM_LINE] [CHUNK_SIZE]
   FILENAME="$1" && shift
   START_FROM_LINE="$1" && shift || START_FROM_LINE=1
   CHUNK_SIZE="$1" && shift || CHUNK_SIZE=10

   FILE_LINES=$(wc -l < "${FILENAME}")

   while [[ ${FILE_LINE} -le ${FILE_LINES} ]]; do
       CHUNK=$(tail -n +"${FILE_LINE}" "${FILENAME}" | head -${CHUNK_SIZE})

       echo "${FILE_LINE} of ${FILE_LINES}"
       echo $"${CHUNK}"  #


read_file_in_chunks "${@}"

Share on: TwitterFacebookGoogle+Email

Related Articles
Go Top