validate result of grep
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm trying with the below code to print Available
if there's a match, else nil
grep -o 'pattern' test.log | awk 'if($0=="pattern") print "Available"; else print "nil"'
The if
part is working fine, but I'm not getting the else
part if the grepped out is null.
awk grep
add a comment |Â
up vote
1
down vote
favorite
I'm trying with the below code to print Available
if there's a match, else nil
grep -o 'pattern' test.log | awk 'if($0=="pattern") print "Available"; else print "nil"'
The if
part is working fine, but I'm not getting the else
part if the grepped out is null.
awk grep
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying with the below code to print Available
if there's a match, else nil
grep -o 'pattern' test.log | awk 'if($0=="pattern") print "Available"; else print "nil"'
The if
part is working fine, but I'm not getting the else
part if the grepped out is null.
awk grep
I'm trying with the below code to print Available
if there's a match, else nil
grep -o 'pattern' test.log | awk 'if($0=="pattern") print "Available"; else print "nil"'
The if
part is working fine, but I'm not getting the else
part if the grepped out is null.
awk grep
awk grep
edited Aug 12 at 13:04
Jeff Schaller
32.4k849110
32.4k849110
asked Aug 7 at 8:21
user304107
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
If the pattern did not match, then grep
produces no output and the awk
program has no data to work with. This is why you will never get nil
from the awk
code.
Another way of doing this would be
if grep -q 'pattern' test.log; then
echo 'Available'
else
echo 'nil'
fi
The -q
option of grep
is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep
, only its exit status.
With awk
, you could still do your test if you wish, but you will have to output the nil
string conditionally in an END
block:
grep -o 'pattern' test.log |
awk '/pattern/ print "Available"; found = 1
END if (!found) print "nil" '
The END
block will be executed even if the awk
script does not get any input to work with.
In fact, you could do the whole thing with awk
:
awk '/pattern/ print "Available"; found = 1; exit
END if (!found) print "nil" ' test.log
Calling exit
will invoke the END
block, so we can't get rid of the found
variable.
i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
â user304107
Aug 7 at 9:12
@reddingr Why would you want a one-liner? You could usegrep -q 'pattern' test.log && echo 'Available' || echo 'nil'
if you feel so inclined.
â Kusalananda
Aug 7 at 9:15
Great!! not aware of option-q
... Thanks.
â user304107
Aug 7 at 9:21
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
If the pattern did not match, then grep
produces no output and the awk
program has no data to work with. This is why you will never get nil
from the awk
code.
Another way of doing this would be
if grep -q 'pattern' test.log; then
echo 'Available'
else
echo 'nil'
fi
The -q
option of grep
is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep
, only its exit status.
With awk
, you could still do your test if you wish, but you will have to output the nil
string conditionally in an END
block:
grep -o 'pattern' test.log |
awk '/pattern/ print "Available"; found = 1
END if (!found) print "nil" '
The END
block will be executed even if the awk
script does not get any input to work with.
In fact, you could do the whole thing with awk
:
awk '/pattern/ print "Available"; found = 1; exit
END if (!found) print "nil" ' test.log
Calling exit
will invoke the END
block, so we can't get rid of the found
variable.
i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
â user304107
Aug 7 at 9:12
@reddingr Why would you want a one-liner? You could usegrep -q 'pattern' test.log && echo 'Available' || echo 'nil'
if you feel so inclined.
â Kusalananda
Aug 7 at 9:15
Great!! not aware of option-q
... Thanks.
â user304107
Aug 7 at 9:21
add a comment |Â
up vote
2
down vote
accepted
If the pattern did not match, then grep
produces no output and the awk
program has no data to work with. This is why you will never get nil
from the awk
code.
Another way of doing this would be
if grep -q 'pattern' test.log; then
echo 'Available'
else
echo 'nil'
fi
The -q
option of grep
is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep
, only its exit status.
With awk
, you could still do your test if you wish, but you will have to output the nil
string conditionally in an END
block:
grep -o 'pattern' test.log |
awk '/pattern/ print "Available"; found = 1
END if (!found) print "nil" '
The END
block will be executed even if the awk
script does not get any input to work with.
In fact, you could do the whole thing with awk
:
awk '/pattern/ print "Available"; found = 1; exit
END if (!found) print "nil" ' test.log
Calling exit
will invoke the END
block, so we can't get rid of the found
variable.
i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
â user304107
Aug 7 at 9:12
@reddingr Why would you want a one-liner? You could usegrep -q 'pattern' test.log && echo 'Available' || echo 'nil'
if you feel so inclined.
â Kusalananda
Aug 7 at 9:15
Great!! not aware of option-q
... Thanks.
â user304107
Aug 7 at 9:21
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
If the pattern did not match, then grep
produces no output and the awk
program has no data to work with. This is why you will never get nil
from the awk
code.
Another way of doing this would be
if grep -q 'pattern' test.log; then
echo 'Available'
else
echo 'nil'
fi
The -q
option of grep
is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep
, only its exit status.
With awk
, you could still do your test if you wish, but you will have to output the nil
string conditionally in an END
block:
grep -o 'pattern' test.log |
awk '/pattern/ print "Available"; found = 1
END if (!found) print "nil" '
The END
block will be executed even if the awk
script does not get any input to work with.
In fact, you could do the whole thing with awk
:
awk '/pattern/ print "Available"; found = 1; exit
END if (!found) print "nil" ' test.log
Calling exit
will invoke the END
block, so we can't get rid of the found
variable.
If the pattern did not match, then grep
produces no output and the awk
program has no data to work with. This is why you will never get nil
from the awk
code.
Another way of doing this would be
if grep -q 'pattern' test.log; then
echo 'Available'
else
echo 'nil'
fi
The -q
option of grep
is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep
, only its exit status.
With awk
, you could still do your test if you wish, but you will have to output the nil
string conditionally in an END
block:
grep -o 'pattern' test.log |
awk '/pattern/ print "Available"; found = 1
END if (!found) print "nil" '
The END
block will be executed even if the awk
script does not get any input to work with.
In fact, you could do the whole thing with awk
:
awk '/pattern/ print "Available"; found = 1; exit
END if (!found) print "nil" ' test.log
Calling exit
will invoke the END
block, so we can't get rid of the found
variable.
edited Aug 7 at 9:10
answered Aug 7 at 8:25
Kusalananda
106k14209327
106k14209327
i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
â user304107
Aug 7 at 9:12
@reddingr Why would you want a one-liner? You could usegrep -q 'pattern' test.log && echo 'Available' || echo 'nil'
if you feel so inclined.
â Kusalananda
Aug 7 at 9:15
Great!! not aware of option-q
... Thanks.
â user304107
Aug 7 at 9:21
add a comment |Â
i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
â user304107
Aug 7 at 9:12
@reddingr Why would you want a one-liner? You could usegrep -q 'pattern' test.log && echo 'Available' || echo 'nil'
if you feel so inclined.
â Kusalananda
Aug 7 at 9:15
Great!! not aware of option-q
... Thanks.
â user304107
Aug 7 at 9:21
i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
â user304107
Aug 7 at 9:12
i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
â user304107
Aug 7 at 9:12
@reddingr Why would you want a one-liner? You could use
grep -q 'pattern' test.log && echo 'Available' || echo 'nil'
if you feel so inclined.â Kusalananda
Aug 7 at 9:15
@reddingr Why would you want a one-liner? You could use
grep -q 'pattern' test.log && echo 'Available' || echo 'nil'
if you feel so inclined.â Kusalananda
Aug 7 at 9:15
Great!! not aware of option
-q
... Thanks.â user304107
Aug 7 at 9:21
Great!! not aware of option
-q
... Thanks.â user304107
Aug 7 at 9:21
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460997%2fvalidate-result-of-grep%23new-answer', 'question_page');
);
Post as a guest
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
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
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