用其他数值替换某列中的数值

时间:2021-08-29 07:51:27

I have data as below:

我有以下数据:

83997000|17561815|20370101000000 83997000|3585618|20370101000000 
83941746|13898890|20361231230000 83940169|13842974|20171124205011 
83999444|3585618|20370101000000 83943970|10560874|20370101000000 
83942000|13898890|20371232230000 83999333|3585618|20350101120000

Now, what I want to achieve is as below:

现在,我想要实现的目标如下:

If column 2 is 17561815, print 22220 to replace 17561815.

如果第2列是17561815,则打印22220以替换17561815。

If column 2 is 3585618, print 23330 to replace 3585618.

如果第2列是3585618,则打印23330以替换3585618。

If column 2 is 13898890, print 24440 to replace 13898890.

如果第2列是13898890,则打印24440以替换13898890。

If column 2 is 13842974, print 25550 to replace 13842974.

如果第2列是13842974,则打印25550以替换13842974。

If column 2 is 3585618, print 26660 to replace 3585618.

如果第2列是3585618,则打印26660以替换3585618。

If column 2 is 10560874, print 27770 to replace 10560874.

如果第2列是10560874,则打印27770以替换10560874。

Output to be like this:

输出如下:

83997000|22220|20370101000000 83997000|23330|20370101000000 
83941746|24440|20361231230000 83940169|25550|20171124205011 
83999444|26660|20370101000000 83943970|27770|20370101000000 
83942000|24440|20371232230000 83999333|26660|20350101120000

1 个解决方案

#1


3  

awk solution:

awk 'BEGIN{ 
         FS=OFS="|"; 
         a["17561815"]=22220; a["13898890"]=24440; 
         a["3585618"]=26660; a["13842974"]=25550; 
         a["10560874"]=27770 
     }
     $2 in a{ $2=a[$2] }
     $4 in a{ $4=a[$4] }1' file

The output:

83997000|22220|20370101000000 83997000|26660|20370101000000 
83941746|24440|20361231230000 83940169|25550|20171124205011 
83999444|26660|20370101000000 83943970|27770|20370101000000 
83942000|24440|20371232230000 83999333|26660|20350101120000

#1


3  

awk solution:

awk 'BEGIN{ 
         FS=OFS="|"; 
         a["17561815"]=22220; a["13898890"]=24440; 
         a["3585618"]=26660; a["13842974"]=25550; 
         a["10560874"]=27770 
     }
     $2 in a{ $2=a[$2] }
     $4 in a{ $4=a[$4] }1' file

The output:

83997000|22220|20370101000000 83997000|26660|20370101000000 
83941746|24440|20361231230000 83940169|25550|20171124205011 
83999444|26660|20370101000000 83943970|27770|20370101000000 
83942000|24440|20371232230000 83999333|26660|20350101120000