Assuming all the files have a 20 line header, and 12 line footer, you can use sed to extract the 21st line through the 13th to last line:
for file in ABC*; do
numlines=$(cat $file | wc -l)
lastline=$(( $numlines - 12 ))
(( 21 <= $lastline )) && sed "21,$lastline \!D" $file >> combined.txt
done
Files that only have the header and footer, but no additional lines, produce no output.
If you prefer to use your head
and tail
commands instead of sed
:
for file in ABC*; do
numlines=$(cat $file | wc -l)
(( 32 < $numlines )) && head -n -12 $file | tail -n +20 >> combined.txt
done