problem with if else statement in awk











up vote
0
down vote

favorite












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?










share|improve this question




















  • 1




    $NF ~ /[0-9]/
    – steeldriver
    Sep 16 '16 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
    Sep 16 '16 at 14:57















up vote
0
down vote

favorite












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?










share|improve this question




















  • 1




    $NF ~ /[0-9]/
    – steeldriver
    Sep 16 '16 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
    Sep 16 '16 at 14:57













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question















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?







text-processing awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 at 22:30









Rui F Ribeiro

38.3k1477127




38.3k1477127










asked Sep 16 '16 at 11:59









vic-3

1033




1033








  • 1




    $NF ~ /[0-9]/
    – steeldriver
    Sep 16 '16 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
    Sep 16 '16 at 14:57














  • 1




    $NF ~ /[0-9]/
    – steeldriver
    Sep 16 '16 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
    Sep 16 '16 at 14:57








1




1




$NF ~ /[0-9]/
– steeldriver
Sep 16 '16 at 12:02




$NF ~ /[0-9]/
– steeldriver
Sep 16 '16 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
Sep 16 '16 at 14:57




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
Sep 16 '16 at 14:57










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










You don't need explicit If/Else logic:



awk '$NF ~ /[0-9]/ {print $NF; next}; {print $(NF-1)}'





share|improve this answer























  • doesn't print what OP wants
    – Rahul
    Sep 16 '16 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
    – steeldriver
    Sep 16 '16 at 12:25











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f310326%2fproblem-with-if-else-statement-in-awk%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










You don't need explicit If/Else logic:



awk '$NF ~ /[0-9]/ {print $NF; next}; {print $(NF-1)}'





share|improve this answer























  • doesn't print what OP wants
    – Rahul
    Sep 16 '16 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
    – steeldriver
    Sep 16 '16 at 12:25















up vote
4
down vote



accepted










You don't need explicit If/Else logic:



awk '$NF ~ /[0-9]/ {print $NF; next}; {print $(NF-1)}'





share|improve this answer























  • doesn't print what OP wants
    – Rahul
    Sep 16 '16 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
    – steeldriver
    Sep 16 '16 at 12:25













up vote
4
down vote



accepted







up vote
4
down vote



accepted






You don't need explicit If/Else logic:



awk '$NF ~ /[0-9]/ {print $NF; next}; {print $(NF-1)}'





share|improve this answer














You don't need explicit If/Else logic:



awk '$NF ~ /[0-9]/ {print $NF; next}; {print $(NF-1)}'






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 16 '16 at 12:19









chaos

34.8k773115




34.8k773115










answered Sep 16 '16 at 12:13









ocurran

1942




1942












  • doesn't print what OP wants
    – Rahul
    Sep 16 '16 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
    – steeldriver
    Sep 16 '16 at 12:25


















  • doesn't print what OP wants
    – Rahul
    Sep 16 '16 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
    – steeldriver
    Sep 16 '16 at 12:25
















doesn't print what OP wants
– Rahul
Sep 16 '16 at 12:16




doesn't print what OP wants
– Rahul
Sep 16 '16 at 12:16




1




1




@Rahul the output appears to be consistent with the OP's textual description - their attempted solution appears to have inverted the logic
– steeldriver
Sep 16 '16 at 12:25




@Rahul the output appears to be consistent with the OP's textual description - their attempted solution appears to have inverted the logic
– steeldriver
Sep 16 '16 at 12:25


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f310326%2fproblem-with-if-else-statement-in-awk%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List directoties down one level, excluding some named directories and files

list processes belonging to a network namespace

list systemd RuntimeDirectory mounts