fastest way convert tab-delimited file to csv in linux

前端 未结 11 1311
感情败类
感情败类 2020-12-04 07:56

I have a tab-delimited file that has over 200 million lines. What\'s the fastest way in linux to convert this to a csv file? This file does have multiple lines of header i

11条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 08:35

    assuming you don't want to change header and assuming you don't have embedded tabs

    # cat file
    header  header  header
    one     two     three
    
    $ awk 'NR>1{$1=$1}1' OFS="," file
    header  header  header
    one,two,three
    

    NR>1 skips the first header. you mentioned you know how many lines of header, so use the correct number for your own case. with this, you also do not need to call any other external commands. just one awk command does the job.

    another way if you have blank columns and you care about that.

    awk 'NR>1{gsub("\t",",")}1' file
    

    using sed

    sed '2,$y/\t/,/' file #skip 1 line header and translate (same as tr)
    

提交回复
热议问题