Bash: Building flat files technique

Building flat files placing characters at certain positions in very long lines can be a tedious task. Using the technique below, such a task can be accomplished easier.

1. Create a file

touch result_file

2. Use a for loop with a range corresponding to the number of lines in the resulting file (the are 10 lines in this example):

for i in {1..10}; do

3. The essence of this technique is the way of using the formatted output and to continuously append it the the resulting file. So, for example, we add two fields values: id, filling positions from 1 to 10, and language, filling positions from 11 to 13:

printf "id12345678" >> "${result_file}" #Id, AN10
printf "eng" >> "${result_file}" #Language, A3

4. printf allows us to use a very compact way to print lists of repetitive characters. For example, we add 50 spaces to the line:

printf "%0.s " {1..50} >> "${result_file}" #Optional field value, AN50

5. Another example of a field value of length 10, populated with zeroes, followed by the loop index value:

printf "%0.s0" {1..8} >> "${result_file}"
if [ ${i} -lt 9 ]; then
  printf "0$((${i}+1))" >> "${result_file}"
else
  printf "$((${i}+1))" >> "${result_file}"
fi #Dynamically constructed value, N10

6. Finish the line by a new line character:

printf '\n' >> "${result_file}"

7. Finish the loop:

done