Convert text file into a comma delimited string

前端 未结 5 763
南旧
南旧 2021-02-06 17:25

I don\'t seem to locate an SO question that matches this exact problem.

I have a text file that has one text token per line, without any commas, tabs, or quotes. I want

5条回答
  •  广开言路
    2021-02-06 17:53

    One way with Awk would be to reset the RS and treat the records as separated by blank lines. This would handle words with spaces and format them in CSV format as expected.

    awk '{$1=$1}1' FS='\n' OFS=',' RS= file
    

    The {$1=$1} is a way to reconstruct the fields in each line($0) of the file based on modifications to Field (FS/OFS) and/or Record separators(RS/ORS). The trailing 1 is to print every line with the modifications done inside {..}.

提交回复
热议问题