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

You can do it by storing the numbers in an array that you output in an END block:

{
    $1 = ""
    count[++n] = gsub("o", "")
}

END {
    $0 = ""                   # empty the current record
    for (i = 1; i <= n; ++i)
        $i = count[i]         # create new fields for output

    # output the current record (created above)
    print
}

Or, you can output the numbers as you calculate them, separated by the field delimiter if needed, and then finish the line in the END block:

{
    $1 = ""

    # Print the number, preceded by OFS if this is not the first line.
    # Does not output a newline (ORS).
    printf "%s%s", (NR > 1 ? OFS : ""), gsub("o", "")
}

END {
    # Finish the output (ORS is a newline by default).
    printf "%s", ORS
}

Both of these variants allow the user to set OFS and ORS to values appropriate to them on the command line, e.g.

$ awk -v OFS=',' -f script.awk file
2,1
$ awk -v OFS='\t' -f script.awk file
2       1
$ awk -v OFS='-->' -v ORS='.\n' -f script.awk file
2-->1.

Note: I'm assuming that the output shown in the question is wrong as it's what you would get when not ignoring the first field (and ignoring the case of the o letter).

You can do it by storing the numbers in an array that you output in an END block:

{
    $1 = ""
    count[++n] = gsub("o", "")
}

END {
    $0 = ""                   # empty the current record
    for (i = 1; i <= n; ++i)
        $i = count[i]         # create new fields for output

    # output the current record (created above)
    print
}

Or, you can output the numbers as you calculate them, separated by the field delimiter if needed, and then finish the line in the END block:

{
    $1 = ""

    # Print the number, preceded by OFS if this is not the first line.
    # Does not output a newline (ORS).
    printf "%s%s", (NR > 1 ? OFS : ""), gsub("o", "")
}

END {
    # Finish the output (ORS is a newline by default).
    printf "%s", ORS
}

Both of these variants allow the user to set OFS and ORS to values appropriate to them on the command line, e.g.

$ awk -v OFS=',' -f script.awk file
2,1
$ awk -v OFS='\t' -f script.awk file
2       1
$ awk -v OFS='-->' -v ORS='.\n' -f script.awk file
2-->1.

Note: I'm assuming that the output shown in the question is wrong as it's what you would get when not ignoring the first field.

You can do it by storing the numbers in an array that you output in an END block:

{
    $1 = ""
    count[++n] = gsub("o", "")
}

END {
    $0 = ""                   # empty the current record
    for (i = 1; i <= n; ++i)
        $i = count[i]         # create new fields for output

    # output the current record (created above)
    print
}

Or, you can output the numbers as you calculate them, separated by the field delimiter if needed, and then finish the line in the END block:

{
    $1 = ""

    # Print the number, preceded by OFS if this is not the first line.
    # Does not output a newline (ORS).
    printf "%s%s", (NR > 1 ? OFS : ""), gsub("o", "")
}

END {
    # Finish the output (ORS is a newline by default).
    printf "%s", ORS
}

Both of these variants allow the user to set OFS and ORS to values appropriate to them on the command line, e.g.

$ awk -v OFS=',' -f script.awk file
2,1
$ awk -v OFS='\t' -f script.awk file
2       1
$ awk -v OFS='-->' -v ORS='.\n' -f script.awk file
2-->1.

Note: I'm assuming that the output shown in the question is wrong as it's what you would get when not ignoring the first field (and ignoring the case of the o letter).

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

You can do it by storing the numbers in an array that you output in an END block:

{
    $1 = ""
    count[++n] = gsub("o", "")
}

END {
    $0 = ""                   # empty the current record
    for (i = 1; i <= n; ++i)
        $i = count[i]         # create new fields for output

    # output the current record (created above)
    print
}

Or, you can output the numbers as you calculate them, separated by the field delimiter if needed, and then finish the line in the END block:

{
    $1 = ""

    # Print the number, preceded by OFS if this is not the first line.
    # Does not output a newline (ORS).
    printf "%s%s", (NR > 1 ? OFS : ""), gsub("o", "")
}

END {
    # Finish the output (ORS is a newline by default).
    printf "%s", ORS
}

Both of these variants allow the user to set OFS and ORS to values appropriate to them on the command line, e.g.

$ awk -v OFS=',' -f script.awk file
2,1
$ awk -v OFS='\t' -f script.awk file
2       1
$ awk -v OFS='-->' -v ORS='.\n' -f script.awk file
2-->1.

Note: I'm assuming that the output shown in the question is wrong as it's what you would get when not ignoring the first field.

You can do it by storing the numbers in an array that you output in an END block:

{
    $1 = ""
    count[++n] = gsub("o", "")
}

END {
    $0 = ""                   # empty the current record
    for (i = 1; i <= n; ++i)
        $i = count[i]         # create new fields for output

    # output the current record (created above)
    print
}

Or, you can output the numbers as you calculate them, separated by the field delimiter if needed, and then finish the line in the END block:

{
    $1 = ""

    # Print the number, preceded by OFS if this is not the first line.
    # Does not output a newline (ORS).
    printf "%s%s", (NR > 1 ? OFS : ""), gsub("o", "")
}

END {
    # Finish the output (ORS is a newline by default).
    printf "%s", ORS
}

Both of these variants allow the user to set OFS and ORS to values appropriate to them on the command line, e.g.

$ awk -v OFS=',' -f script.awk file
2,1
$ awk -v OFS='\t' -f script.awk file
2       1
$ awk -v OFS='-->' -v ORS='.\n' -f script.awk file
2-->1.

You can do it by storing the numbers in an array that you output in an END block:

{
    $1 = ""
    count[++n] = gsub("o", "")
}

END {
    $0 = ""                   # empty the current record
    for (i = 1; i <= n; ++i)
        $i = count[i]         # create new fields for output

    # output the current record (created above)
    print
}

Or, you can output the numbers as you calculate them, separated by the field delimiter if needed, and then finish the line in the END block:

{
    $1 = ""

    # Print the number, preceded by OFS if this is not the first line.
    # Does not output a newline (ORS).
    printf "%s%s", (NR > 1 ? OFS : ""), gsub("o", "")
}

END {
    # Finish the output (ORS is a newline by default).
    printf "%s", ORS
}

Both of these variants allow the user to set OFS and ORS to values appropriate to them on the command line, e.g.

$ awk -v OFS=',' -f script.awk file
2,1
$ awk -v OFS='\t' -f script.awk file
2       1
$ awk -v OFS='-->' -v ORS='.\n' -f script.awk file
2-->1.

Note: I'm assuming that the output shown in the question is wrong as it's what you would get when not ignoring the first field.

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

You can do it by storing the numbers in an array that you output in an END block:

{
    $1 = ""
    count[++n] = gsub("o", "")
}

END {
    $0 = ""                   # empty the current record
    for (i = 1; i <= n; ++i)
        $i = count[i]         # create new fields for output

    # output the current record (created above)
    print
}

Or, you can output the numbers as you calculate them, separated by the field delimiter if needed, and then finish the line in the END block:

{
    $1 = "" 

    # Print the number, preceded by OFS if this is not the first line.
    # Does not output a newline (ORS).
    printf "%s%s", (NR > 1 ? OFS : ""), gsub("o", "")
}

END {
    # Finish the output (ORS is a newline by default).
    printf "%s", ORS
}

Both of these variants allow the user to set OFS and ORS to values appropriate to them on the command line, e.g.

$ awk -v OFS=',' -f script.awk file
2,1
$ awk -v OFS='\t' -f script.awk file
2       1
$ awk -v OFS='-->' -v ORS='.\n' -f script.awk file
2-->1.

You can do it by storing the numbers in an array that you output in an END block:

{
    $1 = ""
    count[++n] = gsub("o", "")
}

END {
    $0 = ""                   # empty the current record
    for (i = 1; i <= n; ++i)
        $i = count[i]         # create new fields for output

    print
}

Or, you can output the numbers as you calculate them, separated by the field delimiter if needed, and then finish the line in the END block:

{
    $1 = ""
    printf "%s%s", (NR > 1 ? OFS : ""), gsub("o", "")
}

END {
    printf "%s", ORS
}

Both of these variants allow the user to set OFS and ORS to values appropriate to them on the command line, e.g.

$ awk -v OFS=',' -f script.awk file
2,1
$ awk -v OFS='\t' -f script.awk file
2       1
$ awk -v OFS='-->' -v ORS='.\n' -f script.awk file
2-->1.

You can do it by storing the numbers in an array that you output in an END block:

{
    $1 = ""
    count[++n] = gsub("o", "")
}

END {
    $0 = ""                   # empty the current record
    for (i = 1; i <= n; ++i)
        $i = count[i]         # create new fields for output

    # output the current record (created above)
    print
}

Or, you can output the numbers as you calculate them, separated by the field delimiter if needed, and then finish the line in the END block:

{
    $1 = "" 

    # Print the number, preceded by OFS if this is not the first line.
    # Does not output a newline (ORS).
    printf "%s%s", (NR > 1 ? OFS : ""), gsub("o", "")
}

END {
    # Finish the output (ORS is a newline by default).
    printf "%s", ORS
}

Both of these variants allow the user to set OFS and ORS to values appropriate to them on the command line, e.g.

$ awk -v OFS=',' -f script.awk file
2,1
$ awk -v OFS='\t' -f script.awk file
2       1
$ awk -v OFS='-->' -v ORS='.\n' -f script.awk file
2-->1.
added 183 characters in body
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Loading
Source Link
Kusalananda
  • 356.2k
  • 42
  • 737
  • 1.1k
Loading