0

I want to convert all text files in a directory to one csv file. I want the input to the csv to be the text in the text file labeled by the author of the text file found in the filename. For reference, this is what some filenames look like:

'Winston Churchill___The Crisis, Complete.txt'
'Winston Churchill___The Crossing.txt'
'Winston Churchill___The Dwelling Place of Light, Complete.txt'
'Winston Churchill___The Inside of the Cup, Complete.txt'
'Zane Grey___Betty Zane.txt'
'Zane Grey___Desert Gold.txt'
'Zane Grey___Riders of the Purple Sage.txt'

Example output would be:

column1     column2                     
Author1     text written by author 1......   
Author1     text written by author 1......   
Author2     text written by author 2......   
Author2     text written by author 2......              

Edit: test text.... entails 1000 characters of text written by the author in the corresponding column.

2
  • 1
    Edit your question and add samples of what you want to data to look like in the output. More clarity is needed on what test text..... is. Also, does every file have a set of underscores between the author and the title? – Nasir Riley Jun 30 '20 at 5:32
  • @NasirRiley test text.... entails 1000 characters of text extracted from a book written by the author in the corresponding column. Yes, every filename begins with the author followed by underscores. – Myth Jun 30 '20 at 5:53
3

With ksh93 instead of bash which has builtin support for printing csv with its %#q printf format (and a more efficient $(<file)):

for file in *___*.txt; do
  printf "%#q,%#q\n" "${file%%___*}" "$(<"$file")"
done >> file.csv
5
  • I am getting this error: "ksh: line 2: : cannot open". – Myth Jun 30 '20 at 6:14
  • @Myth, sorry, had the wrong variable names in the body of the loop. – Stéphane Chazelas Jun 30 '20 at 6:18
  • Now I'm getting this error: ksh: line 2: ___.txt: cannot open [No such file or directory]. Not sure what this means because there are files in the current directory. – Myth Jun 30 '20 at 6:25
  • @Myth, that script has to be run from within the directory that contains those *___*.txt files. Here the syntax is the same as the bash syntax except for those %#q. bash copied most of its syntax from ksh. – Stéphane Chazelas Jun 30 '20 at 6:25
  • Fixed it. It works great, thanks. – Myth Jun 30 '20 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.