-2

I need an output in below format:

Consider for example a sequence of numbers from 1 to 50 in my input file, each number representing a line of data:

$ cat input.txt
1
2
3
.
.
.
49
50

I want them to be in 5 files, each file having 10 lines:

$ cat output1.txt
1
2
.
.
.
9
10

$ cat output2.txt
11
12
.
.
.
19
20 

And so on. I want to do this via a Bash script, without using the split command.

closed as unclear what you're asking by techraf, Jeff Schaller, HalosGhost, Archemar, Eric Renouf Oct 17 '16 at 14:05

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What exactly should each file have inside of it? Please be more clear. – phk Oct 17 '16 at 11:50
  • example: file_a.txt has 1000 lines each line is of same size. I want them to be split in 100 files each having 10 files using bash script rather than using split command. Hope this make things clear. – New_to_python Oct 17 '16 at 11:52
  • Please edit your question and show us your expected output. Also explain why you need this to be done in bash instead of other tools. – terdon Oct 17 '16 at 11:53
  • Can you paste sample output ? OR do you want like seq 10 > file_a,txt type output – SHW Oct 17 '16 at 11:55
  • Seq 1 100 is being generated to a file.txt but i want them to be split into small files consisting 5 lines example: cat file_a.txt 1 2 3 4 5 cat file_b.txt 6 7 8 9 10 – New_to_python Oct 17 '16 at 11:59
1

An awk approach:

awk -va=1 '{ print > a; if(NR % 10 ==0){a++} }' input 

This will produce files 1, 2, 3, 4 and 5. To make it print to output1 through 5, use this instead:

awk -va=1 '{ print > "output"a; if(NR % 10 ==0){a++} }' input 

Explanation

  • -va=1 : set the variable a to 1.
  • print > "output"a; : print the current line to a file called output and the current value of the variable a.
  • if(NR % 10 ==0){a++} : NR is the current line number. So, if NR is a multiple of 10 (if NR modulo 10 is 0), increment the value of the variable a by one. This ensures that the output file's number changes as necessary.
  • Very neat awk solution, would definitely recommend this over my head/tail loop answer ! +1 – Valentin B. Oct 17 '16 at 13:45
0

Obviously you can do it with split (https://stackoverflow.com/questions/2016894/how-to-split-a-large-text-file-into-smaller-files-with-equal-number-of-lines) but if you want to do it with a loop instead:

numLines=$(cat input.txt | wc -l)
size=50
n=$(( numLines / size ))

for (( i=0; i<n; i++ )); do
     j=$(( i*size + 1 ))
     tail -n +$j input.txt | head -n $size > output_$i.txt
done

In one-liner format :

numLines=$(cat input.txt | wc -l); for (( i=0; i<$(( numLines / 50 )); i++ )); \
    do tail -n +$(( i*50 + 1)) input.txt | head -n 50 > output_$i.txt; done

Not the answer you're looking for? Browse other questions tagged or ask your own question.