Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用UNIX/Linux操作.csv文件的特定列(排除行)_Regex_Linux_Csv_Unix_Data Manipulation - Fatal编程技术网

Regex 使用UNIX/Linux操作.csv文件的特定列(排除行)

Regex 使用UNIX/Linux操作.csv文件的特定列(排除行),regex,linux,csv,unix,data-manipulation,Regex,Linux,Csv,Unix,Data Manipulation,我想访问和操作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

我想访问和操作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

如果我没有弄错你的问题,你能试着回答以下问题吗。如果代码显示任何行的第二个字段中没有精确的3个字符,则不要打印该行

awk 'BEGIN{FS=","} $2~/^[a-zA-Z]{3}$/' Input_file
如果您有旧的
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个字母值的行:

awk --re-interval -F, 'tolower($2) ~ /^[a-z]{3}$/' country.txt
也可以检查长度,但这样可以确保只提供3个字母

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

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

print
是awk中的默认操作,因此
tolower($2)~/^[a-z]{3}$/
是表示
tolower($2)~/^[a-z]{3}$/{print}


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

国家/地区代码是否也必须是列表中的第二项,或者它是否可以出现在行中的任何位置?能否请您提及您的示例预期输出?请说明删除帖子最后一列的逻辑。Country_代码是.csv文件的第二列。我需要第二列中的代码,保持相同的结构更有意义。输出集:卢森堡,卢森堡,20179294689.12阿鲁巴,ABW,2017927865.82尼泊尔,NPL,20179028196.37孟加拉国,BGD,201788057460.51哥斯达黎加,CRI,20178695008.05智利,CHL,201784603249.72库克群岛,库克群岛,201782045。41@JohnTipotas,但问题仍然是一样的,删除第四列的逻辑是什么,条件是什么?请同时提及这些条件,然后让我们知道。是的,你说得对,抱歉,我没有说得更具体。我希望我的输出数据集在每一列中都有正确的格式,以便我可以处理它(我希望第二列正好有3个字符/国家/地区代码)。此外,您的建议非常有效。非常感谢。@Johntipota,是的,这应该适合您,代码只查找3个字符,让我也添加字符验证。@Johntipota,请检查我的编辑,并让我知道这是否有助于您?下一步,请务必在这里提及否决投票的原因?
——re interval
是awk的旧版本,您不需要新版本。