2

I am trying to create a TSV file from an array that I build inside a loop. I do get the values on each line to be tab separated, but I am not able to export each element of the array on a new line. This is an example:

OUTPUT=()
#header
OUTPUT+=$(printf "col_1\tcol_2\tcol_3")

param_1="bla"
param_2="tra"
param_3="meh"

for i in 1 .. 3
  do
    OUTPUT+=$(printf "$param_1\t$param_2\t$param_3")
done
#export
printf '%s\n' "${OUTPUT[@]}" > test.tsv

I have also tried to put \n at the end of each string that I insert in the array, but it did not work. Any idea what I am doing wrong? Thank you

5
  • 1
    if it's going to end up in a file, there is no need to have the intermediate array layer, you can directly write the elements to the file. If you don't want the in-the-flight file to be visible, you can use a temp file and move to final name at the end. Commented Feb 17, 2017 at 15:26
  • 1
    yes, if you intend to use scripting in the future, this is not the way to be writing to files (although, yes, you can). Better to spend your time learning a tool designed for manipulating data (and reading/writing to files). Check out awk tutorial or if you need unlimited control and system features, OO, etc etc, find a python or perl tutorial that works for you. Good luck. Commented Feb 17, 2017 at 15:30
  • @karakfa I would avoid using a temp file and write at every iteration in it... why bother.. even if it is buffered and my output will not exceed half an MB. I can very well output only once at the end, no? Commented Feb 17, 2017 at 15:39
  • @shellter thanks for your comment. For this simple example I have no need to use awk or other dedicated tools. If I need to get something more complex done, then of course I would use something more appropriate. I just needed to write a script to be in line with the other existing scripts and not use a different programming language/tool just for a simple task Commented Feb 17, 2017 at 15:42
  • the reason is not exposing in-the-flight file to other processes prematurely. It's an integrity issue rather then performance. However, may not be necessary in your particular case. Commented Feb 17, 2017 at 15:43

1 Answer 1

3

To append to an array you should use this syntax:

array+=(content)

Also there is no need to use printf for appending the static text.

Here is a working script:

OUTPUT=()
#header
OUTPUT+=("col_1\tcol_2\tcol_3")

param_1="bla"
param_2="tra"
param_3="meh"

for i in {1..3}
do
    OUTPUT+=("$param_1\t$param_2\t$param_3")
done
#export
printf '%b\n' "${OUTPUT[@]}" > test.tsv

Note use of %b in printf so that escape sequences are interpreted correctly.

Output:

cat test.tsv

col_1   col_2   col_3
bla     tra     meh
bla     tra     meh
bla     tra     meh
Sign up to request clarification or add additional context in comments.

1 Comment

Hi. Thanks for the answer. It works now. So, the thing that actually makes a difference is the conversion to binary, %b in the printf exporting to file which will expand all escape characters. I have tried before without using printf while adding elements to the array but it would print all the string with \t. Thanks once again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.