简体   繁体   English

当有多个字段分隔符时,使用 AWK 忽略字段中的逗号

[英]Ignoring commas within fields using AWK when there are multiple field separators

I want to parse CSV records like the one below with awk or gawk .我想用awkgawk解析像下面这样的 CSV 记录。

The fields are separated by commas but the last field ( $6 ) is special because it really consists of subfields.字段由逗号分隔,但最后一个字段 ( $6 ) 很特殊,因为它实际上由子字段组成。 These subfields are separated by # as the field separator (or, to be precise, ". # ").这些子字段由 # 分隔,作为字段分隔符(或者,准确地说是“.#”)。 This in itself is not a problem: I can use awk -F'(,)|(. # )' to set alternative field separators.这本身不是问题:我可以使用awk -F'(,)|(. # )'来设置替代字段分隔符。

However, there are stray commas in this last field as well that need to be ignored.但是,在最后一个字段中也有需要忽略的杂散逗号。

Is there a way to solve this with awk , perhaps using FPAT?有没有办法用awk解决这个问题,也许使用 FPAT?

Sample record:样本记录:

  "http://publications.europa.eu/resource/cellar/3befa3c3-a9af-4dac-baa2-92e95cb6e3ab","http://publications.europa.eu/resource/cellar/3befa3c3-a9af-4dac-baa2-92e95cb6e3ab.0002","EU:C:1985:443","61984CJ0239","Gerlach","Judgment of the Court (Third Chamber) of 24 October 1985. # Gerlach & Co. BV, Internationale Expeditie, v Minister van Economische Zaken. # Reference for a preliminary ruling: College van Beroep voor het Bedrijfsleven - Netherlands. # Article 41 ECSC - Anti-dumping duties. # Case 239/84."

Using FPAT feature in gnu-awk , you may be able to do this.使用gnu-awk中的FPAT功能,您也许可以做到这一点。 We use FPAT to match all double quoted fields or comma separated fields.我们使用FPAT来匹配所有双引号字段或逗号分隔的字段。 Finally we split on last field using /\. # /最后,我们使用/\. # / /\. # / regex pattern. /\. # /正则表达式模式。

s='"http://publications.europa.eu/resource/cellar/3befa3c3-a9af-4dac-baa2-92e95cb6e3ab","http://publications.europa.eu/resource/cellar/3befa3c3-a9af-4dac-baa2-92e95cb6e3ab.0002","EU:C:1985:443","61984CJ0239","Gerlach","Judgment of the Court (Third Chamber) of 24 October 1985. # Gerlach & Co. BV, Internationale Expeditie, v Minister van Economische Zaken. # Reference for a preliminary ruling: College van Beroep voor het Bedrijfsleven - Netherlands. # Article 41 ECSC - Anti-dumping duties. # Case 239/84."'

awk -v FPAT='"[^"]*"|[^,]+' '{
   # loop through all fields except last one
   for (i=1; i<NF; ++i)
      print i, $i
   # split last field using /\. # / regex and print each token
   for (j=1; j<split($NF, a, /\. # /); ++j)
      print i+j-1, a[j]
}' <<< "$s"

1 "http://publications.europa.eu/resource/cellar/3befa3c3-a9af-4dac-baa2-92e95cb6e3ab"
2 "http://publications.europa.eu/resource/cellar/3befa3c3-a9af-4dac-baa2-92e95cb6e3ab.0002"
3 "EU:C:1985:443"
4 "61984CJ0239"
5 "Gerlach"
6 "Judgment of the Court (Third Chamber) of 24 October 1985
7 Gerlach & Co. BV, Internationale Expeditie, v Minister van Economische Zaken
8 Reference for a preliminary ruling: College van Beroep voor het Bedrijfsleven - Netherlands
9 Article 41 ECSC - Anti-dumping duties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM