Unix code wanted to change column values

I have a file that looks like this:

ID1 ID2 3 ID2 ID3 3 ID4 ID5 3 ID6 ID7 4 ID8 ID9 4 ID8 ID6 4 

And I want to change it to:

 ID1 ID2 1 ID2 ID3 1 ID4 ID5 1 ID6 ID7 2 ID8 ID9 2 ID8 ID6 2 

So, I want to change all 3 and 4 from the third column to 1 and 2 respectively. What is the most efficient way to do this for a very large file? Thank you very much in advance!

+4
source share
2 answers
 awk '{ $3 -= 2; print }' filename >new_filename 

Or, if you really only want to touch 3 and 4:

 awk '{ if ( $3 == 3 ) { $3 = 1 } else if ( $3 == 4 ) { $3 = 2 }; print}' filename >new_filename 
+6
source

Assuming the third column is always the last:

 sed 's/\([^0-9]\)3$/\11/;s/\([^0-9]\)4$/\12/' 
+1
source

Source: https://habr.com/ru/post/1413042/


All Articles