Certainly! You can create a small bash function to achieve this. Below is a sample function that you can add to your .bashrc or .bash_profile or simply run in your terminal session:
get_nth_chunk() {
local filename=$1
local chunk_number=$2
local lines_per_chunk=10
local start_line=$(( (chunk_number - 1) * lines_per_chunk + 1 ))
local end_line=$(( start_line + lines_per_chunk - 1 ))
# Use 'sed' to print the range of lines
sed -n "${start_line},${end_line}p" "$filename"
}
Here's what each part does:
filename=$1 - This sets the first argument passed to the function as the filename.
chunk_number=$2 - This sets the second argument passed to the function as the chunk number to retrieve.
lines_per_chunk=10 - This sets the number of lines per chunk.
start_line=$(( (chunk_number - 1) * lines_per_chunk + 1 )) - This calculates the starting line number for the desired chunk.
end_line=$(( start_line + lines_per_chunk - 1 )) - This calculates the ending line number for the desired chunk.
sed -n "${start_line},${end_line}p" "$filename" - This uses sed to print out the range of lines from the file.
To use this function after adding it to your bash configuration, you would source your .bashrc or .bash_profile or simply paste the function into your terminal, then you can call it like so:
get_nth_chunk /tmp/my_134_lines_file 3
This command will print the lines from 21 to 30 (the 3rd chunk of 10 lines) of the file /tmp/my_134_lines_file.
Remember that bash functions and variables are case-sensitive, and line numbers in files start from 1, not 0. If you want to include the last chunk which contains fewer lines (in case the file does not divide evenly), the function already handles this by specifying the range to sed, which will print whatever lines are available without error.