Convert text file into a comma delimited string

前端 未结 5 684
南旧
南旧 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 18:02

    With Perl one-liner:

    $ cat csv_2_text
    one
    two
    three
    $ perl -ne '{ chomp; push(@lines,$_) } END { $x=join(",",@lines);  print "$x" }' csv_2_text
    one,two,three
    
    $ perl -ne ' { chomp; $_="$_," if not eof ;printf("%s",$_) } ' csv_2_text
    one,two,three
    $
    

    From @codeforester

    $ perl -ne 'BEGIN { my $delim = "" } { chomp; printf("%s%s", $delim, $_); $delim="," } END { printf("\n") }' csv_2_text
    one,two,three
    $
    

提交回复
热议问题