How to move grep inside awk script?
In the below have I 3 grep
commands that I would like to replace with awk's grep. so I have tried
! /000000000000/;
! /000000000000/ $0;
! /000000000000/ $3;
where I don't get an error, but testing with both the script below and
$ echo 000000000000 | awk ' ! /000000000000/; print '
000000000000
it doesn't skip the lines as expected.
Question
Can anyone explain why my "not grep" doesn't work in awk
?
grep -v '^#' $hosts | grep -E '[0-9A-F]12b' | grep -v 000000000000 | awk '
print "host "$5" "
print " option host-name ""$5"";"
gsub(/..B/,"&:", $3)
print " hardware ethernet "$3";"
print " fixed-address "$1";"
print ""
print ""
' > /etc/dhcp/reservations.conf
awk
add a comment |
In the below have I 3 grep
commands that I would like to replace with awk's grep. so I have tried
! /000000000000/;
! /000000000000/ $0;
! /000000000000/ $3;
where I don't get an error, but testing with both the script below and
$ echo 000000000000 | awk ' ! /000000000000/; print '
000000000000
it doesn't skip the lines as expected.
Question
Can anyone explain why my "not grep" doesn't work in awk
?
grep -v '^#' $hosts | grep -E '[0-9A-F]12b' | grep -v 000000000000 | awk '
print "host "$5" "
print " option host-name ""$5"";"
gsub(/..B/,"&:", $3)
print " hardware ethernet "$3";"
print " fixed-address "$1";"
print ""
print ""
' > /etc/dhcp/reservations.conf
awk
Do you want to print a line only if it doesn't match that pattern? If so, you need! /blah/ print
. Note the pattern is outside the action block.
– Shawn
Nov 12 '18 at 11:54
1
@Sandra Schlichting, you need NOT to use multiplegrep
withawk
. Good that you showed your attempts in post. If you could simply post sample of input and sample of expected output then we could do it in singleawk
too.
– RavinderSingh13
Nov 12 '18 at 11:57
@Sandra Schlichting, without seeing yourreservations.conf
file it is difficult to tell what is not working in addition to my previous comments.
– RavinderSingh13
Nov 12 '18 at 12:00
add a comment |
In the below have I 3 grep
commands that I would like to replace with awk's grep. so I have tried
! /000000000000/;
! /000000000000/ $0;
! /000000000000/ $3;
where I don't get an error, but testing with both the script below and
$ echo 000000000000 | awk ' ! /000000000000/; print '
000000000000
it doesn't skip the lines as expected.
Question
Can anyone explain why my "not grep" doesn't work in awk
?
grep -v '^#' $hosts | grep -E '[0-9A-F]12b' | grep -v 000000000000 | awk '
print "host "$5" "
print " option host-name ""$5"";"
gsub(/..B/,"&:", $3)
print " hardware ethernet "$3";"
print " fixed-address "$1";"
print ""
print ""
' > /etc/dhcp/reservations.conf
awk
In the below have I 3 grep
commands that I would like to replace with awk's grep. so I have tried
! /000000000000/;
! /000000000000/ $0;
! /000000000000/ $3;
where I don't get an error, but testing with both the script below and
$ echo 000000000000 | awk ' ! /000000000000/; print '
000000000000
it doesn't skip the lines as expected.
Question
Can anyone explain why my "not grep" doesn't work in awk
?
grep -v '^#' $hosts | grep -E '[0-9A-F]12b' | grep -v 000000000000 | awk '
print "host "$5" "
print " option host-name ""$5"";"
gsub(/..B/,"&:", $3)
print " hardware ethernet "$3";"
print " fixed-address "$1";"
print ""
print ""
' > /etc/dhcp/reservations.conf
awk
awk
edited Nov 12 '18 at 12:20
RavinderSingh13
25.9k41438
25.9k41438
asked Nov 12 '18 at 11:51
Sandra SchlichtingSandra Schlichting
9,1142678126
9,1142678126
Do you want to print a line only if it doesn't match that pattern? If so, you need! /blah/ print
. Note the pattern is outside the action block.
– Shawn
Nov 12 '18 at 11:54
1
@Sandra Schlichting, you need NOT to use multiplegrep
withawk
. Good that you showed your attempts in post. If you could simply post sample of input and sample of expected output then we could do it in singleawk
too.
– RavinderSingh13
Nov 12 '18 at 11:57
@Sandra Schlichting, without seeing yourreservations.conf
file it is difficult to tell what is not working in addition to my previous comments.
– RavinderSingh13
Nov 12 '18 at 12:00
add a comment |
Do you want to print a line only if it doesn't match that pattern? If so, you need! /blah/ print
. Note the pattern is outside the action block.
– Shawn
Nov 12 '18 at 11:54
1
@Sandra Schlichting, you need NOT to use multiplegrep
withawk
. Good that you showed your attempts in post. If you could simply post sample of input and sample of expected output then we could do it in singleawk
too.
– RavinderSingh13
Nov 12 '18 at 11:57
@Sandra Schlichting, without seeing yourreservations.conf
file it is difficult to tell what is not working in addition to my previous comments.
– RavinderSingh13
Nov 12 '18 at 12:00
Do you want to print a line only if it doesn't match that pattern? If so, you need
! /blah/ print
. Note the pattern is outside the action block.– Shawn
Nov 12 '18 at 11:54
Do you want to print a line only if it doesn't match that pattern? If so, you need
! /blah/ print
. Note the pattern is outside the action block.– Shawn
Nov 12 '18 at 11:54
1
1
@Sandra Schlichting, you need NOT to use multiple
grep
with awk
. Good that you showed your attempts in post. If you could simply post sample of input and sample of expected output then we could do it in single awk
too.– RavinderSingh13
Nov 12 '18 at 11:57
@Sandra Schlichting, you need NOT to use multiple
grep
with awk
. Good that you showed your attempts in post. If you could simply post sample of input and sample of expected output then we could do it in single awk
too.– RavinderSingh13
Nov 12 '18 at 11:57
@Sandra Schlichting, without seeing your
reservations.conf
file it is difficult to tell what is not working in addition to my previous comments.– RavinderSingh13
Nov 12 '18 at 12:00
@Sandra Schlichting, without seeing your
reservations.conf
file it is difficult to tell what is not working in addition to my previous comments.– RavinderSingh13
Nov 12 '18 at 12:00
add a comment |
2 Answers
2
active
oldest
votes
Could you please try changing your code to:
echo 000000000000 | awk '!/000000000000/'
Problem in your attempt: $ echo 000000000000 | awk ' ! /000000000000/; print '
Since you are checking condition ! /000000000000/
which is having ;
after it so that condition works well and DO NOT print anything. But then you have print
after it which is NOT COMING under that condition so it simply prints that line.
awk
works on patternaction
if you are putting semi colon in between it means that condition ends before it and statement after ;
is all together a new statements for awk
.
EDIT: Adding possible solution by seeing OP's attempt here, not tested at all since no samples are shown by OP. Also I am using --re-interval
since my awk
version is old you could remove in case you have new version of awk
in your box.
awk --re-interval '!/^#/ && !/000000000000/ && /[0-9A-Fa-f]12/
print "host "$5" "
print " option host-name ""$5"";"
gsub(/..B/,"&:", $3)
print " hardware ethernet "$3";"
print " fixed-address "$1";"
print ""
print ""
' "$host" > /etc/dhcp/reservations.conf
I were clearly hoping thatawk
worked similar togrep
by going to next input line, if the searhed didn't match. In regards topatternaction
I did try/something/ next
as this blog post shows. Why doesn't this work? blog.jpalardy.com/posts/skip-grep-use-awk
– Sandra Schlichting
Nov 12 '18 at 13:23
1
@SandraSchlichting, so AFAIU your requirements, if you need NOT to print line which is having000000000000
so either you could do/000000000000/next
OR!/000000000000/
both should work. But if you do!/000000000000/next
this will print000000000000
line, let me know if I made you my point clear here?
– RavinderSingh13
Nov 12 '18 at 13:26
add a comment |
Taking a look at your code:
$ echo 000000000000 | awk '
! /000000000000/ # on given input this evaluates to false
# but since its in action, affects nothing
print # this prints the record regardless of whatever happened above
'
Adding a print
may help you understand:
$ echo 000000000000 | awk ' print ! /000000000000/; print '
0
000000000000
Removing the !
:
$ echo 000000000000 | awk ' print /000000000000/; print '
1
000000000000
This is all I can help you with since there is not enough information for more.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
);
);
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%2fstackoverflow.com%2fquestions%2f53261575%2fhow-to-move-grep-inside-awk-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Could you please try changing your code to:
echo 000000000000 | awk '!/000000000000/'
Problem in your attempt: $ echo 000000000000 | awk ' ! /000000000000/; print '
Since you are checking condition ! /000000000000/
which is having ;
after it so that condition works well and DO NOT print anything. But then you have print
after it which is NOT COMING under that condition so it simply prints that line.
awk
works on patternaction
if you are putting semi colon in between it means that condition ends before it and statement after ;
is all together a new statements for awk
.
EDIT: Adding possible solution by seeing OP's attempt here, not tested at all since no samples are shown by OP. Also I am using --re-interval
since my awk
version is old you could remove in case you have new version of awk
in your box.
awk --re-interval '!/^#/ && !/000000000000/ && /[0-9A-Fa-f]12/
print "host "$5" "
print " option host-name ""$5"";"
gsub(/..B/,"&:", $3)
print " hardware ethernet "$3";"
print " fixed-address "$1";"
print ""
print ""
' "$host" > /etc/dhcp/reservations.conf
I were clearly hoping thatawk
worked similar togrep
by going to next input line, if the searhed didn't match. In regards topatternaction
I did try/something/ next
as this blog post shows. Why doesn't this work? blog.jpalardy.com/posts/skip-grep-use-awk
– Sandra Schlichting
Nov 12 '18 at 13:23
1
@SandraSchlichting, so AFAIU your requirements, if you need NOT to print line which is having000000000000
so either you could do/000000000000/next
OR!/000000000000/
both should work. But if you do!/000000000000/next
this will print000000000000
line, let me know if I made you my point clear here?
– RavinderSingh13
Nov 12 '18 at 13:26
add a comment |
Could you please try changing your code to:
echo 000000000000 | awk '!/000000000000/'
Problem in your attempt: $ echo 000000000000 | awk ' ! /000000000000/; print '
Since you are checking condition ! /000000000000/
which is having ;
after it so that condition works well and DO NOT print anything. But then you have print
after it which is NOT COMING under that condition so it simply prints that line.
awk
works on patternaction
if you are putting semi colon in between it means that condition ends before it and statement after ;
is all together a new statements for awk
.
EDIT: Adding possible solution by seeing OP's attempt here, not tested at all since no samples are shown by OP. Also I am using --re-interval
since my awk
version is old you could remove in case you have new version of awk
in your box.
awk --re-interval '!/^#/ && !/000000000000/ && /[0-9A-Fa-f]12/
print "host "$5" "
print " option host-name ""$5"";"
gsub(/..B/,"&:", $3)
print " hardware ethernet "$3";"
print " fixed-address "$1";"
print ""
print ""
' "$host" > /etc/dhcp/reservations.conf
I were clearly hoping thatawk
worked similar togrep
by going to next input line, if the searhed didn't match. In regards topatternaction
I did try/something/ next
as this blog post shows. Why doesn't this work? blog.jpalardy.com/posts/skip-grep-use-awk
– Sandra Schlichting
Nov 12 '18 at 13:23
1
@SandraSchlichting, so AFAIU your requirements, if you need NOT to print line which is having000000000000
so either you could do/000000000000/next
OR!/000000000000/
both should work. But if you do!/000000000000/next
this will print000000000000
line, let me know if I made you my point clear here?
– RavinderSingh13
Nov 12 '18 at 13:26
add a comment |
Could you please try changing your code to:
echo 000000000000 | awk '!/000000000000/'
Problem in your attempt: $ echo 000000000000 | awk ' ! /000000000000/; print '
Since you are checking condition ! /000000000000/
which is having ;
after it so that condition works well and DO NOT print anything. But then you have print
after it which is NOT COMING under that condition so it simply prints that line.
awk
works on patternaction
if you are putting semi colon in between it means that condition ends before it and statement after ;
is all together a new statements for awk
.
EDIT: Adding possible solution by seeing OP's attempt here, not tested at all since no samples are shown by OP. Also I am using --re-interval
since my awk
version is old you could remove in case you have new version of awk
in your box.
awk --re-interval '!/^#/ && !/000000000000/ && /[0-9A-Fa-f]12/
print "host "$5" "
print " option host-name ""$5"";"
gsub(/..B/,"&:", $3)
print " hardware ethernet "$3";"
print " fixed-address "$1";"
print ""
print ""
' "$host" > /etc/dhcp/reservations.conf
Could you please try changing your code to:
echo 000000000000 | awk '!/000000000000/'
Problem in your attempt: $ echo 000000000000 | awk ' ! /000000000000/; print '
Since you are checking condition ! /000000000000/
which is having ;
after it so that condition works well and DO NOT print anything. But then you have print
after it which is NOT COMING under that condition so it simply prints that line.
awk
works on patternaction
if you are putting semi colon in between it means that condition ends before it and statement after ;
is all together a new statements for awk
.
EDIT: Adding possible solution by seeing OP's attempt here, not tested at all since no samples are shown by OP. Also I am using --re-interval
since my awk
version is old you could remove in case you have new version of awk
in your box.
awk --re-interval '!/^#/ && !/000000000000/ && /[0-9A-Fa-f]12/
print "host "$5" "
print " option host-name ""$5"";"
gsub(/..B/,"&:", $3)
print " hardware ethernet "$3";"
print " fixed-address "$1";"
print ""
print ""
' "$host" > /etc/dhcp/reservations.conf
edited Nov 12 '18 at 12:07
answered Nov 12 '18 at 11:53
RavinderSingh13RavinderSingh13
25.9k41438
25.9k41438
I were clearly hoping thatawk
worked similar togrep
by going to next input line, if the searhed didn't match. In regards topatternaction
I did try/something/ next
as this blog post shows. Why doesn't this work? blog.jpalardy.com/posts/skip-grep-use-awk
– Sandra Schlichting
Nov 12 '18 at 13:23
1
@SandraSchlichting, so AFAIU your requirements, if you need NOT to print line which is having000000000000
so either you could do/000000000000/next
OR!/000000000000/
both should work. But if you do!/000000000000/next
this will print000000000000
line, let me know if I made you my point clear here?
– RavinderSingh13
Nov 12 '18 at 13:26
add a comment |
I were clearly hoping thatawk
worked similar togrep
by going to next input line, if the searhed didn't match. In regards topatternaction
I did try/something/ next
as this blog post shows. Why doesn't this work? blog.jpalardy.com/posts/skip-grep-use-awk
– Sandra Schlichting
Nov 12 '18 at 13:23
1
@SandraSchlichting, so AFAIU your requirements, if you need NOT to print line which is having000000000000
so either you could do/000000000000/next
OR!/000000000000/
both should work. But if you do!/000000000000/next
this will print000000000000
line, let me know if I made you my point clear here?
– RavinderSingh13
Nov 12 '18 at 13:26
I were clearly hoping that
awk
worked similar to grep
by going to next input line, if the searhed didn't match. In regards to patternaction
I did try /something/ next
as this blog post shows. Why doesn't this work? blog.jpalardy.com/posts/skip-grep-use-awk– Sandra Schlichting
Nov 12 '18 at 13:23
I were clearly hoping that
awk
worked similar to grep
by going to next input line, if the searhed didn't match. In regards to patternaction
I did try /something/ next
as this blog post shows. Why doesn't this work? blog.jpalardy.com/posts/skip-grep-use-awk– Sandra Schlichting
Nov 12 '18 at 13:23
1
1
@SandraSchlichting, so AFAIU your requirements, if you need NOT to print line which is having
000000000000
so either you could do /000000000000/next
OR !/000000000000/
both should work. But if you do !/000000000000/next
this will print 000000000000
line, let me know if I made you my point clear here?– RavinderSingh13
Nov 12 '18 at 13:26
@SandraSchlichting, so AFAIU your requirements, if you need NOT to print line which is having
000000000000
so either you could do /000000000000/next
OR !/000000000000/
both should work. But if you do !/000000000000/next
this will print 000000000000
line, let me know if I made you my point clear here?– RavinderSingh13
Nov 12 '18 at 13:26
add a comment |
Taking a look at your code:
$ echo 000000000000 | awk '
! /000000000000/ # on given input this evaluates to false
# but since its in action, affects nothing
print # this prints the record regardless of whatever happened above
'
Adding a print
may help you understand:
$ echo 000000000000 | awk ' print ! /000000000000/; print '
0
000000000000
Removing the !
:
$ echo 000000000000 | awk ' print /000000000000/; print '
1
000000000000
This is all I can help you with since there is not enough information for more.
add a comment |
Taking a look at your code:
$ echo 000000000000 | awk '
! /000000000000/ # on given input this evaluates to false
# but since its in action, affects nothing
print # this prints the record regardless of whatever happened above
'
Adding a print
may help you understand:
$ echo 000000000000 | awk ' print ! /000000000000/; print '
0
000000000000
Removing the !
:
$ echo 000000000000 | awk ' print /000000000000/; print '
1
000000000000
This is all I can help you with since there is not enough information for more.
add a comment |
Taking a look at your code:
$ echo 000000000000 | awk '
! /000000000000/ # on given input this evaluates to false
# but since its in action, affects nothing
print # this prints the record regardless of whatever happened above
'
Adding a print
may help you understand:
$ echo 000000000000 | awk ' print ! /000000000000/; print '
0
000000000000
Removing the !
:
$ echo 000000000000 | awk ' print /000000000000/; print '
1
000000000000
This is all I can help you with since there is not enough information for more.
Taking a look at your code:
$ echo 000000000000 | awk '
! /000000000000/ # on given input this evaluates to false
# but since its in action, affects nothing
print # this prints the record regardless of whatever happened above
'
Adding a print
may help you understand:
$ echo 000000000000 | awk ' print ! /000000000000/; print '
0
000000000000
Removing the !
:
$ echo 000000000000 | awk ' print /000000000000/; print '
1
000000000000
This is all I can help you with since there is not enough information for more.
edited Nov 12 '18 at 12:12
answered Nov 12 '18 at 12:05
James BrownJames Brown
18.3k31635
18.3k31635
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53261575%2fhow-to-move-grep-inside-awk-script%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
Do you want to print a line only if it doesn't match that pattern? If so, you need
! /blah/ print
. Note the pattern is outside the action block.– Shawn
Nov 12 '18 at 11:54
1
@Sandra Schlichting, you need NOT to use multiple
grep
withawk
. Good that you showed your attempts in post. If you could simply post sample of input and sample of expected output then we could do it in singleawk
too.– RavinderSingh13
Nov 12 '18 at 11:57
@Sandra Schlichting, without seeing your
reservations.conf
file it is difficult to tell what is not working in addition to my previous comments.– RavinderSingh13
Nov 12 '18 at 12:00