Skip to main content
Post Made Community Wiki by don_crissti
added 345 characters in body
Source Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262

You could splitsplit the 2nd field on : and if you get more than 2 pieces (that is, the number of elements in array z) keep only the 1st one:

awk '{n=split($2, z, ":");if (n > 2) $2=z[1]};1' infile 

If you wanted to use sub you could do something like:

awk '{sub(/:.*:.*/,"",$2)};1' infile 

that is, attempt to replace two colons (or more).

You could split the 2nd field on : and if you get more than 2 pieces keep only the 1st one:

awk '{n=split($2, z, ":");if (n > 2) $2=z[1]};1' infile 

You could split the 2nd field on : and if you get more than 2 pieces (that is, the number of elements in array z) keep only the 1st one:

awk '{n=split($2, z, ":");if (n > 2) $2=z[1]};1' infile 

If you wanted to use sub you could do something like:

awk '{sub(/:.*:.*/,"",$2)};1' infile 

that is, attempt to replace two colons (or more).

Source Link
don_crissti
  • 85.7k
  • 31
  • 234
  • 262

You could split the 2nd field on : and if you get more than 2 pieces keep only the 1st one:

awk '{n=split($2, z, ":");if (n > 2) $2=z[1]};1' infile