Awk consider double quoted string as one token and ignore space in between

前端 未结 7 1025
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 17:26

Data file - data.txt:

ABC \"I am ABC\" 35 DESC
DEF \"I am not ABC\" 42 DESC

cat data.txt | awk \'{print $2}\'

will re

7条回答
  •  失恋的感觉
    2020-12-15 18:14

    Another alternative would be to use the FPAT variable, that defines a regular expression describing the contents of each field.

    Save this AWK script as parse.awk:

    #!/bin/awk -f
    
    BEGIN {
      FPAT = "([^ ]+)|(\"[^\"]+\")"
    }
    {
      print $2
    }
    

    Make it executable with chmod +x ./parse.awk and parse your data file as ./parse.awk data.txt:

    "I am ABC"
    "I am not ABC"
    

提交回复
热议问题