I receive a csv file which always contains the same number of rows but never the same number of columns (Sometimes I have 3 columns, and sometimes it can go up to 12 or more!).
The file looks like this:
cat file.csv
John-;Paul-;Lisa-;Tim-
21-;44-;25-;33-
London-;Paris-;Chicago-;Roma-
Student;Teacher;Engineer;Cook
Funny-;Clever-;Sincere-;Passionate-
I wish to write in a text file the content of each column in a precise order while respecting one column per line, for example:
John-London-21-Funny-Student
Paul-Paris-44-Clever-Teacher
Lisa-Chicago-25-Sincer-Engineer
Tim-Roma-33-Passionate-Cook
I wrote this bash script:
cat file.csv | awk -F";" '{ print $1 }' > temp1
declare -a lines
readarray -t lines <temp1
echo -n "${lines[0]}" > result.txt
echo -n "${lines[2]}" >> result.txt
echo -n "${lines[1]}" >> result.txt
echo -n "${lines[4]}" >> result.txt
echo -n "${lines[3]}" >> result.txt
The result is correct because I get this:
cat result.txt
John-London-21-Student
...but I only get the first occurrence, I don't know how to loop the awk command and increment it to read all the columns of the file.
Do you have any ideas?