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?
text-processing awk
add a comment |
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?
text-processing awk
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:NFto$NF,=to~and variables to print are exchanged... @ocurran's answer is a good alternative
– Sundeep
Sep 16 '16 at 14:57
add a comment |
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?
text-processing awk
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
text-processing awk
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:NFto$NF,=to~and variables to print are exchanged... @ocurran's answer is a good alternative
– Sundeep
Sep 16 '16 at 14:57
add a comment |
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:NFto$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
add a comment |
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)}'
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
add a comment |
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)}'
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
add a comment |
up vote
4
down vote
accepted
You don't need explicit If/Else logic:
awk '$NF ~ /[0-9]/ {print $NF; next}; {print $(NF-1)}'
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
add a comment |
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)}'
You don't need explicit If/Else logic:
awk '$NF ~ /[0-9]/ {print $NF; next}; {print $(NF-1)}'
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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:NFto$NF,=to~and variables to print are exchanged... @ocurran's answer is a good alternative– Sundeep
Sep 16 '16 at 14:57