Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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
String 如何根据模式将文件顶部的字符串替换为底部字符串_String_Bash_Replace - Fatal编程技术网

String 如何根据模式将文件顶部的字符串替换为底部字符串

String 如何根据模式将文件顶部的字符串替换为底部字符串,string,bash,replace,String,Bash,Replace,我有以下文件: petro,36262 artur,946034 alex,12345 alex,99999 artur,111111111 彼得罗,36262 阿图尔,946034 亚历克斯,12345 亚历克斯,99999 阿图尔,111111 我需要的是,基于第一列作为主键,将上面的字符串替换为下面的字符串,因此最终看起来如下所示: petro,36262 alex,99999 artur,111111111 彼得罗,36262 亚历克斯,99999 阿图尔,111111 第一列可以包含空

我有以下文件:

petro,36262 artur,946034 alex,12345 alex,99999 artur,111111111 彼得罗,36262 阿图尔,946034 亚历克斯,12345 亚历克斯,99999 阿图尔,111111 我需要的是,基于第一列作为主键,将上面的字符串替换为下面的字符串,因此最终看起来如下所示:

petro,36262 alex,99999 artur,111111111 彼得罗,36262 亚历克斯,99999 阿图尔,111111 第一列可以包含空格或数字

谢谢这里有一个方法:

$ awk -F, '{a[$1]=$2} END{for (name in a)print name","a[name]}' file
alex,99999
artur,111111111
petro,36262
这些线是按任意顺序排出来的。如果您有首选订单,则需要一些附加代码