Extract middle section of lines of a text file?
If you want not to get messed up but still do it using tail
and head
, there is a useful way of invoking tail
using a line-count from the beginning, not the end:
tail -n +4001 yourfile | head -4000
... But a better, automatic tool made just for splitting files is called... split
! It's also a part of GNU coreutils, so any normal Linux system should have it. Here's how you can use it:
split -l 4000 yourInputFile thePrefixForOutputFiles
(See man split
if in doubt.)
Combining head and tail as you did will work, but for this I would use sed
sed -n '1,4000p' input_file # print lines 1-4000 of input_file
This lets you solve your problem with a quick shell function
chunk_it(){
step=4
start=1
end=$step
for n in {1..4} ; do
sed -n "${start},${end}p" "$1" > "$1".$start-$end
let start+=$step
let end+=$step
done
}
chunk_it your_file
Now you have your_file.1-4000 and yuor_file.4001-8000 and so on.
Note: requires bash