To Replace Numerical Values in a certain column with other Numerical Values

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.

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

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

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

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

If column 2 is 10560874, print 27770 to replace 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 answer

  • answered 2018-01-11 19:44 RomanPerekhrest

    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