Skip to content
Advertisement

Recode value in the column in unix with awk or sed

In the following file, The values of 6th column for the rows who have values other than 1 or 2 in sixth column should be replaced with -9. How can I do it?
old.fam

JavaScript

new.fam should be

JavaScript

Edit: I used cat old.fam | awk '{ if ($6==1 || $6==2) {print $1 " " $2 " " $3 " " $4 " " $5 " " $6 ;} else {print $1 " " $2 " " $3 " " $4 " " $5 " " -9;}}'> new.fam

Now the problem is the rows with replaced 6th column value (-9), does not have space separated FS between 5th and 6th column.

JavaScript

Advertisement

Answer

Here you have something you can start working on:

JavaScript

The awk script does the following:

  • check the value of the sixth column
  • between both checks, there’s the awk || logical OR operator

The rest of the script is obvious.

Edit
Apparently awk can’t handle spaces, followed by numbers, so you might use this awk script:

JavaScript

(Mind the $5 " -9" at the end)

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement