Skip to main content

Bash: Building flat files examples

· 2 min read
Yaroslav Grebnov
Golang developer, SDET

Examples of compact bash scripts for building long flat text files.

  1. Create a new file
touch result_file
  1. Use a for loop with a range corresponding to the number of lines in the resulting file (there are 10 lines in this example):
for i in {1..10}; do
  1. The essence of this technique is the way of using the formatted output and to continuously append it 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
  1. 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
  1. 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
  1. Finish the line by a new line character:
printf '\n' >> "${result_file}"
  1. Finish the loop:
done