convert a fixed width file from text to csv

后端 未结 4 2006
梦谈多话
梦谈多话 2021-02-01 09:59

I have a large data file in text format and I want to convert it to csv by specifying each column length.

number of columns = 5

column length

[4          


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-01 10:38

    Here's a solution that works with regular awk (does not require gawk).

    awk -v OFS=',' '{print substr($0,1,4), substr($0,5,2), substr($0,7,5), substr($0,12,1), substr($0,13,1)}'
    

    It uses awk's substr function to define each field's start position and length. OFS defines what the output field separator is (in this case, a comma).

    (Side note: This only works if the source data does not have any commas. If the data has commas, then you have to escape them to be proper CSV, which is beyond the scope of this question.)

    Demo:

    echo 'aasdfh9013512
    ajshdj 2445df' | 
    awk -v OFS=',' '{print substr($0,1,4), substr($0,5,2), substr($0,7,5), substr($0,12,1), substr($0,13,1)}'
    

    Output:

    aasd,fh,90135,1,2
    ajsh,dj, 2445,d,f
    

提交回复
热议问题