Ignoring embeded spaces with AWK

后端 未结 4 1104
无人及你
无人及你 2020-12-20 08:40

I\'m looking for a simple way to print a specific field with awk while allowing for embedded spaces in the field.

Sample: Field1 Field2 \"Field Three\" Field

4条回答
  •  余生分开走
    2020-12-20 09:08

    You can do this if the double quotes are always there:

    awk -F\" '{print $2}'
    

    Specifically, I am telling awk that the fields are separated by double quotes, at which point the part you want is readily available as field 2.

    If you need to get at subsequent fields, you can split the remainder of the line on spaces and get a new array, say F[] of fields, like this:

    awk -F\" '{split($3,F," ");print $2,F[1],F[2]}' file
    
    Field Three Field4 Field5
    

    assuming your file looks like this:

    Field1 Field2 "Field Three" Field4 Field5 Field6
    

提交回复
热议问题