AWK - How to count all numbers in a file using functions
Clash Royale CLAN TAG#URR8PPP
I'm slightly confused because my pattern works if I don't use functions. On the other hand it doesn't.
Input file:
asd
111
22
3333
mike
44444
mas
My awk function: ( it always prints 1 instead of 4 )
function A()
/[0-9]/ number++
print number
END
A()
Pattern works if I use it outside the function but inside it always prints 1
, what's the problem??
awk
add a comment |
I'm slightly confused because my pattern works if I don't use functions. On the other hand it doesn't.
Input file:
asd
111
22
3333
mike
44444
mas
My awk function: ( it always prints 1 instead of 4 )
function A()
/[0-9]/ number++
print number
END
A()
Pattern works if I use it outside the function but inside it always prints 1
, what's the problem??
awk
add a comment |
I'm slightly confused because my pattern works if I don't use functions. On the other hand it doesn't.
Input file:
asd
111
22
3333
mike
44444
mas
My awk function: ( it always prints 1 instead of 4 )
function A()
/[0-9]/ number++
print number
END
A()
Pattern works if I use it outside the function but inside it always prints 1
, what's the problem??
awk
I'm slightly confused because my pattern works if I don't use functions. On the other hand it doesn't.
Input file:
asd
111
22
3333
mike
44444
mas
My awk function: ( it always prints 1 instead of 4 )
function A()
/[0-9]/ number++
print number
END
A()
Pattern works if I use it outside the function but inside it always prints 1
, what's the problem??
awk
awk
edited Jan 24 at 7:57
Inian
4,355925
4,355925
asked Jan 24 at 7:55
Mark2012Mark2012
61
61
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
A pattern like /[0-9]/
aren't usually used inside a block. They are more commonly used as
pattern block
If you want to test a regular expression against a string inside a function, use match()
:
function A()
if (match($0, "[0-9]"))
++number
A()
END
print number
This would count the number of line that contains at least one digit by calling A()
for each line. The function A()
would match the [0-9]
regular expression against the contents of the line and increase number
by one if it matches. The END
block prints the resulting number
at the end of the execution.
In place of if (match($0, "[0-9]"))
, you could also use if (/[0-9]/)
, but match()
is more flexible.
This has the same effect as the awk
program
/[0-9]/ ++number
END print number
If you want to count the number of lines that contains only a positive integer (instead of lines that contains a digit somewhere), use ^[0-9]+$
as the regular expression instead.
The main point is that the function A()
needs to be called for each line of input, and that the number
variable's value should be printed in the END
block.
The following syntax is accepted by the POSIX awk
grammar, but the program would however not count the number of lines containing a digit, but would count the number of lines.
function A()
/[0-9]/ ++number
A()
END
print number
The statement /[0-9]/ ++number
would evaluate to the character 0
or 1
(depending on whether the current line matches) concatenated with the result of ++number
as a string.
Since you call your A()
function only in the END
block, number
would be incremented exactly once, and this is why you always get 1
as output.
1
/[0-9]/ ++number
is/[0-9]/
(0 or 1) concatenated with++number
. There are cases where/RE/
conflicts with/
(division) when concatenation is involved (like ina /foo/ b
), but that's not one of them.
– Stéphane Chazelas
Jan 24 at 8:36
@StéphaneChazelas Ah. Yes. Thanks!
– Kusalananda
Jan 24 at 8:36
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f496398%2fawk-how-to-count-all-numbers-in-a-file-using-functions%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
A pattern like /[0-9]/
aren't usually used inside a block. They are more commonly used as
pattern block
If you want to test a regular expression against a string inside a function, use match()
:
function A()
if (match($0, "[0-9]"))
++number
A()
END
print number
This would count the number of line that contains at least one digit by calling A()
for each line. The function A()
would match the [0-9]
regular expression against the contents of the line and increase number
by one if it matches. The END
block prints the resulting number
at the end of the execution.
In place of if (match($0, "[0-9]"))
, you could also use if (/[0-9]/)
, but match()
is more flexible.
This has the same effect as the awk
program
/[0-9]/ ++number
END print number
If you want to count the number of lines that contains only a positive integer (instead of lines that contains a digit somewhere), use ^[0-9]+$
as the regular expression instead.
The main point is that the function A()
needs to be called for each line of input, and that the number
variable's value should be printed in the END
block.
The following syntax is accepted by the POSIX awk
grammar, but the program would however not count the number of lines containing a digit, but would count the number of lines.
function A()
/[0-9]/ ++number
A()
END
print number
The statement /[0-9]/ ++number
would evaluate to the character 0
or 1
(depending on whether the current line matches) concatenated with the result of ++number
as a string.
Since you call your A()
function only in the END
block, number
would be incremented exactly once, and this is why you always get 1
as output.
1
/[0-9]/ ++number
is/[0-9]/
(0 or 1) concatenated with++number
. There are cases where/RE/
conflicts with/
(division) when concatenation is involved (like ina /foo/ b
), but that's not one of them.
– Stéphane Chazelas
Jan 24 at 8:36
@StéphaneChazelas Ah. Yes. Thanks!
– Kusalananda
Jan 24 at 8:36
add a comment |
A pattern like /[0-9]/
aren't usually used inside a block. They are more commonly used as
pattern block
If you want to test a regular expression against a string inside a function, use match()
:
function A()
if (match($0, "[0-9]"))
++number
A()
END
print number
This would count the number of line that contains at least one digit by calling A()
for each line. The function A()
would match the [0-9]
regular expression against the contents of the line and increase number
by one if it matches. The END
block prints the resulting number
at the end of the execution.
In place of if (match($0, "[0-9]"))
, you could also use if (/[0-9]/)
, but match()
is more flexible.
This has the same effect as the awk
program
/[0-9]/ ++number
END print number
If you want to count the number of lines that contains only a positive integer (instead of lines that contains a digit somewhere), use ^[0-9]+$
as the regular expression instead.
The main point is that the function A()
needs to be called for each line of input, and that the number
variable's value should be printed in the END
block.
The following syntax is accepted by the POSIX awk
grammar, but the program would however not count the number of lines containing a digit, but would count the number of lines.
function A()
/[0-9]/ ++number
A()
END
print number
The statement /[0-9]/ ++number
would evaluate to the character 0
or 1
(depending on whether the current line matches) concatenated with the result of ++number
as a string.
Since you call your A()
function only in the END
block, number
would be incremented exactly once, and this is why you always get 1
as output.
1
/[0-9]/ ++number
is/[0-9]/
(0 or 1) concatenated with++number
. There are cases where/RE/
conflicts with/
(division) when concatenation is involved (like ina /foo/ b
), but that's not one of them.
– Stéphane Chazelas
Jan 24 at 8:36
@StéphaneChazelas Ah. Yes. Thanks!
– Kusalananda
Jan 24 at 8:36
add a comment |
A pattern like /[0-9]/
aren't usually used inside a block. They are more commonly used as
pattern block
If you want to test a regular expression against a string inside a function, use match()
:
function A()
if (match($0, "[0-9]"))
++number
A()
END
print number
This would count the number of line that contains at least one digit by calling A()
for each line. The function A()
would match the [0-9]
regular expression against the contents of the line and increase number
by one if it matches. The END
block prints the resulting number
at the end of the execution.
In place of if (match($0, "[0-9]"))
, you could also use if (/[0-9]/)
, but match()
is more flexible.
This has the same effect as the awk
program
/[0-9]/ ++number
END print number
If you want to count the number of lines that contains only a positive integer (instead of lines that contains a digit somewhere), use ^[0-9]+$
as the regular expression instead.
The main point is that the function A()
needs to be called for each line of input, and that the number
variable's value should be printed in the END
block.
The following syntax is accepted by the POSIX awk
grammar, but the program would however not count the number of lines containing a digit, but would count the number of lines.
function A()
/[0-9]/ ++number
A()
END
print number
The statement /[0-9]/ ++number
would evaluate to the character 0
or 1
(depending on whether the current line matches) concatenated with the result of ++number
as a string.
Since you call your A()
function only in the END
block, number
would be incremented exactly once, and this is why you always get 1
as output.
A pattern like /[0-9]/
aren't usually used inside a block. They are more commonly used as
pattern block
If you want to test a regular expression against a string inside a function, use match()
:
function A()
if (match($0, "[0-9]"))
++number
A()
END
print number
This would count the number of line that contains at least one digit by calling A()
for each line. The function A()
would match the [0-9]
regular expression against the contents of the line and increase number
by one if it matches. The END
block prints the resulting number
at the end of the execution.
In place of if (match($0, "[0-9]"))
, you could also use if (/[0-9]/)
, but match()
is more flexible.
This has the same effect as the awk
program
/[0-9]/ ++number
END print number
If you want to count the number of lines that contains only a positive integer (instead of lines that contains a digit somewhere), use ^[0-9]+$
as the regular expression instead.
The main point is that the function A()
needs to be called for each line of input, and that the number
variable's value should be printed in the END
block.
The following syntax is accepted by the POSIX awk
grammar, but the program would however not count the number of lines containing a digit, but would count the number of lines.
function A()
/[0-9]/ ++number
A()
END
print number
The statement /[0-9]/ ++number
would evaluate to the character 0
or 1
(depending on whether the current line matches) concatenated with the result of ++number
as a string.
Since you call your A()
function only in the END
block, number
would be incremented exactly once, and this is why you always get 1
as output.
edited Jan 24 at 11:04
answered Jan 24 at 8:01
KusalanandaKusalananda
129k16244402
129k16244402
1
/[0-9]/ ++number
is/[0-9]/
(0 or 1) concatenated with++number
. There are cases where/RE/
conflicts with/
(division) when concatenation is involved (like ina /foo/ b
), but that's not one of them.
– Stéphane Chazelas
Jan 24 at 8:36
@StéphaneChazelas Ah. Yes. Thanks!
– Kusalananda
Jan 24 at 8:36
add a comment |
1
/[0-9]/ ++number
is/[0-9]/
(0 or 1) concatenated with++number
. There are cases where/RE/
conflicts with/
(division) when concatenation is involved (like ina /foo/ b
), but that's not one of them.
– Stéphane Chazelas
Jan 24 at 8:36
@StéphaneChazelas Ah. Yes. Thanks!
– Kusalananda
Jan 24 at 8:36
1
1
/[0-9]/ ++number
is /[0-9]/
(0 or 1) concatenated with ++number
. There are cases where /RE/
conflicts with /
(division) when concatenation is involved (like in a /foo/ b
), but that's not one of them.– Stéphane Chazelas
Jan 24 at 8:36
/[0-9]/ ++number
is /[0-9]/
(0 or 1) concatenated with ++number
. There are cases where /RE/
conflicts with /
(division) when concatenation is involved (like in a /foo/ b
), but that's not one of them.– Stéphane Chazelas
Jan 24 at 8:36
@StéphaneChazelas Ah. Yes. Thanks!
– Kusalananda
Jan 24 at 8:36
@StéphaneChazelas Ah. Yes. Thanks!
– Kusalananda
Jan 24 at 8:36
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.
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%2f496398%2fawk-how-to-count-all-numbers-in-a-file-using-functions%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