【问题标题】:Manipulate(exclude lines) a specific column of a .csv file, using UNIX/Linux使用 UNIX/Linux 操作(排除行) .csv 文件的特定列
【发布时间】:2020-02-27 04:34:16
【问题描述】:

我想访问和操作 csv 文件的第四列。特别是我想排除不符合特定要求的行(排除没有 3 个字符国家代码的行)。

我的数据集:

Luxembourg,LUX,2017,9294689.12
Aruba,ABW,2017,927865.82
Nepal,NPL,2017,9028196.37
Bangladesh,BGD,2017,88057460.51
Costa Rica,CRI,2017,8695008.05
Chile,CHL,2017,84603249.72
Cook Islands,COK,2017,82045.41
World,OWIDWRL,1755,9361520
India,INDIA,1763,0
Asia and Pacific (other),,2017,5071156099
World,OWID_WRL,1752,9354192
Middle East,,1751,0
International transport,,1751,0
India,IND,1751,0
Europe (other),,1751,0
China,CHN,1751,0
Asia and Pacific (other),,1751,0
Americas (other),,1751,0
Africa,,1751,0

提前致谢。

我已经按年份对数据文件进行了排序, 但我不知道如何访问第 4 列并使用 awk 或 sed。

预期数据集:

Luxembourg,LUX,2017,9294689.12
Aruba,ABW,2017,927865.82
Nepal,NPL,2017,9028196.37
Bangladesh,BGD,2017,88057460.51
Costa Rica,CRI,2017,8695008.05
Chile,CHL,2017,84603249.72
Cook Islands,COK,2017,82045.41

【问题讨论】:

  • 国家代码是否也必须是列表中的第二项,还是可以出现在行中的任何位置?
  • 能否请您提及您的示例预期输出?以及删除帖子中最后一列的逻辑应该是什么。
  • Country_Code 是 .csv 文件的第二列。我需要第二列中的代码,保持相同的结构更有意义。
  • 输出集:Luxembourg,LUX,2017,9294689.12 Aruba,ABW,2017,927865.82 Nepal,NPL,2017,9028196.37 Bangladesh,BGD,2017,88057460.51 Costa Rica,CRI,2017,8695008.0 ,2017,84603249.72 Cook Islands,COK,2017,82045.41
  • @JohnTipotas,但问题仍然存在,删除第 4 列的逻辑是什么,有什么条件请提及它们并让我们知道。

标签: regex linux csv unix data-manipulation


【解决方案1】:

如果我没有正确回答您的问题,请您尝试关注。如果任何行的第二个字段中没有精确的 3 个字符,则代码在哪里查看,则不要打印该行。

awk 'BEGIN{FS=","} $2~/^[a-zA-Z]{3}$/' Input_file

如果您有 OLD awk,其中范围 {3} 不起作用,请尝试。

awk 'BEGIN{FS=","} $2~/^[a-zA-Z][a-zA-Z][a-zA-Z]$/' Input_file


说明:在此处添加对上述代码的说明。

awk '                  ##Starting awk program here.
BEGIN{                 ##Starting BEGIN section from here. Which will be executed before Input_file is being read
  FS=","               ##Setting field separator as comma here.
}                      ##Closing BEGIN section here.
$2~/^[a-zA-Z]{3}$/     ##Checking condition if 2nd field is starting with alphabets 3 occurrence of it and ending with it too.
                       ##Since awk works on method of condition then action; so if condition is TRUE then perform certain action.
                       ##In this case no action given so  by default print of line will happen.
' Input_file           ##Mentioning Input_file name here.

【讨论】:

  • 是的,你没看错,抱歉没有更具体。我希望我的输出数据集在每一列中都具有正确的格式,以便我可以处理它(我希望第二列有正好 3 个字符/国家代码)。此外,您的建议非常有效。非常感谢。
  • @JohnTipotas,是的,这应该对你有用,代码只查找 3 个字符,让我也添加字符验证。
  • @JohnTipotas,请检查我的编辑,如果这对您有帮助,请告诉我?
  • DOWN VOTER,请在此提及投反对票的原因?
【解决方案2】:

下面将仅输出第二个字段中具有 3 个字母值的行:

awk --re-interval -F, 'tolower($2) ~ /^[a-z]{3}$/' country.txt

也可以检查长度,但这确保只提供 3 个字母。

--re-internval 允许您在 RE 中使用 itnernval 表达式,因为大括号是 awk 中的保留字符。

-F, 告诉 awk 输入分隔符是逗号。

print 是 awk 中的默认操作,所以 tolower($2) ~ /^[a-z]{3}$/tolower($2) ~ /^[a-z]{3}$/ {print} 的简写方式

tolower($2) 将第二个字段的值小写,~ 是正则表达式比较运算符,我们用它来检查字符串^ 的开头,然后[a-z] 重复{3} 次和字符串$ 的结尾。

【讨论】:

  • --re-interval 是 awk 的旧版本,恕我直言,您不需要新版本。
猜你喜欢
  • 2018-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-02
  • 1970-01-01
  • 1970-01-01
  • 2013-02-08
相关资源
最近更新 更多