separate fields by comma using bash

前端 未结 8 1561
滥情空心
滥情空心 2021-01-25 01:07

How do I place commas between fields?

Input data

12123 \'QA test case 1\' \'QA environment\'   
12234 \'UAT test case 1\' \'UAT environment\'  
         


        
8条回答
  •  粉色の甜心
    2021-01-25 02:01

    Here is how I handle csv with awk

    cat file
    12123 'QA test case 1' 'QA environment' some more
    12234 'UAT test case 1' 'UAT environment'
    

    awk '{for (i=1;i

    This keep track of how many ' it finds.
    If its 0 2 4 6 etc you are outside a group, split using ,
    If its 1 3 5 7 etc you are inside a group, split by (a space)


    Since you now have a good separator, you can get rid of the '

    awk '{for (i=1;i

    You could also use FPAT that is used to define fields, opposed to FS that define separators, but then you need gnu awk 4.x, and it would not be portable.

    awk '{for (i=1;i

    How does the FPAT="[^' ]+|'[^']+'" works?
    1. A field should not contain one or more ' or space. [^' ]+ eks some and more
    2. A field starts with ' then one or more not ' and then ends with '. '[^']+' eks 'test data'

提交回复
热议问题