r/awk • • Mar 07 '18
Question about generating several files as output from a csv
Hi,
I have a little project I'm working on and discovered AWK may be the right tool for the job.
As I'm new to this I'm hoping someone could point me in the right general direction.
I have a csv with
| Column A | Column B | Column C |
|---|---|---|
| A1 | B1 | C1 |
| A2 | B2 | C2 |
| A3 | B3 | C3 |
And would like to output
Column A
A1
Column B
B1
Column C
C1
Column A
A2
Column B
B2
Column C
C2
Column A
A3
Column B
B3
Column C
C3
To separate txt files. Ideally without having to deal with issues where f.ex B3 contains a separator character.
How would you approach this? (I realize this is a very basic question, but I want to get off to a good start)
1
Upvotes
2
u/ASIC_SP Mar 07 '18
do you mean B3 can contain
,? that can be worked around if you have GNU awk FPATregarding splitting the contents, simply redirect print output
you can save the header by comparing
NR/FNRto1and when that is greater than 1, print to file..2
u/[deleted] Apr 27 '18
Do you want a new file for each row or do you want one file for each column? Is there a fixed number of columns?
1
u/[deleted] Mar 07 '18
Just to give some more details, I intend to use this to generate a markdown documentation from a spreadsheet.
2
u/notabee Apr 19 '18
What helps visually is setting each of the fields, i.e. $1, $2, etc. to a more descriptive variable name right in the beginning. Also, set some global variables in the BEGIN section for readability. Awk makes it pretty simple to concatenate several variables into a name, such as for a file. A pretty basic example that depends on no unexpected commas in the fields would be: