Skip to main content
added 32 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

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]/).

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.

added 153 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

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]/).

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]/).

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.

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]/).

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.

added 48 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k

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]/).

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 seems to beis accepted by GNU awk, BSD awk and mawk, but I can't see how the POSIX awk grammar would allow it. The, 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.

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.

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 seems to be accepted by GNU awk, BSD awk and mawk, but I can't see how the POSIX awk grammar would allow it. 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
}

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]/).

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.

added 131 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Loading
added 44 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Loading
added 44 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Loading
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Loading