1

I have a .txt file like this containing six columns. I want to add a seventh column V8 into the file based on the values of column V7_Phenoin a way that Yes should be coded as 2, No as 1 and missing values as -9

V1       V2     V3      V4      V6     V7_Pheno
2253792 20482   NA      DNA     1       Yes
2253802 20491   NA      DNA     4       Yes
2253816 20503   NA      DNA     0       No
2253820 20508   NA      DNA     4       Yes
2253821 20509   NA      DNA     0       No
2253824 20511   NA      DNA     0       No
2253826 20513   NA      DNA     3       Yes
2253829 20516   NA      DNA     6       Yes

The outcome .txt file i am looking for should be like this

V1       V2     V3      V4      V6     V7_Pheno   V8
    2253792 20482   NA      DNA     1       Yes    2
    2253802 20491   NA      DNA     4       Yes    2
    2253816 20503   NA      DNA     0       No     1
    2253820 20508   NA      DNA     4       Yes    2
    2253821 20509   NA      DNA     0       No     1
    2253824 20511   NA      DNA     0       No     1
    2253826 20513   NA      DNA     3       Yes    2
    2253829 20516   NA      DNA     6       Yes    2
2
  • Sounds like a job for "awk". Take a look at this tutorial (or another - you'll find many!), and see if you agree: grymoire.com/Unix/Awk.html Commented Jun 15, 2021 at 0:48
  • Thanks But an example command line would be of great help. Commented Jun 15, 2021 at 0:56

2 Answers 2

3

Using awk:

awk 'NR==1{$7="V8";print;next}\
 $6 == "Yes" {$7="2"};\
 $6 == "No" {$7="1"}1' a.txt |column -t
  1. The first line print the V8 in the header
  2. If the value of 6th col: V7_Pheno is Yes, then $7 = "2"
  3. If the value of 6th col: V7_Pheno is No, then $7 = "1"
  4. column -t : for better formatting
Sign up to request clarification or add additional context in comments.

Comments

1

With Perl:

perl -lane 'print join "\t", @F, $.==1 ? "V8" : $F[5]=~/No/ ? 1 : $F[5]=~/Yes/ ? 2 : -9' file.txt | column -t

print join "\t", @F print the fields of every line, using tab as the field separator.

$.==1 ? "V8" if this is the 1st line, print an additional field V8.

$F[5]=~/No/ ? 1 else, if the 6th field is No, print an additional field 1.

$F[5]=~/Yes/ ? 2 else, if the 6th field is Yes, print an additional field 2.

-9 else, print -9.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.