0

I have a file like this:

Archaea     2   domain      
Archaea Aenigmarchaeota     11084   phylum      123

I am trying to use the if else statement in awk. I want to get the last column of each line and then check:

if{(its is a number) print (column)} else (print the previous colum) 

I have tried this:

awk '{if(NF=/[0-9]/){print $(NF-1)} else{print $(NF)}}' head.txt 

What am I doing wrong?

2
  • 1
    $NF ~ /[0-9]/ Commented Sep 16, 2016 at 12:02
  • awk '{if($NF~/[0-9]/){print $NF} else{print $(NF-1)}}' head.txt .. fixes: NF to $NF , = to ~ and variables to print are exchanged... @ocurran's answer is a good alternative
    – Sundeep
    Commented Sep 16, 2016 at 14:57

1 Answer 1

4

You don't need explicit If/Else logic:

awk '$NF ~ /[0-9]/ {print $NF; next}; {print $(NF-1)}'
2
  • doesn't print what OP wants
    – Rahul
    Commented Sep 16, 2016 at 12:16
  • 1
    @Rahul the output appears to be consistent with the OP's textual description - their attempted solution appears to have inverted the logic Commented Sep 16, 2016 at 12:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .