Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8671389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:56:19+00:00 2026-06-12T18:56:19+00:00

I have a text file with words and positive numbers, separated by some whitespace,

  • 0

I have a text file with words and positive numbers, separated by some whitespace, e.g.

A dog has a ball number 49     number    34 number    A
Cats number   58
...

I want to sum up all the numbers that occur after the string “number”. If after a string “number” is not a number, then it doesn’t matter.

For example, in this case the answer would be 49+34+58, which is 141.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:56:20+00:00Added an answer on June 12, 2026 at 6:56 pm
    awk '{ for (i = 1; i <= NF; i++) s = s+$i }; END { print s+0 }' test.txt
    

    Awk reads the file, line per line. For every line, the blocks marked by {} are executed. blocks can be guarded by a condition: a regular expression, …, and BEGIN and END, which are ‘true’ for the first line, and the last line, respectively.

    This means that awk exeutes the first block for every line (because it is unguarded).

    Furthermore, awk does not really have a type system — all strings. But you can use arithmetic on the strings – in which case they are magically converted to numbers. If you do arithmetic on strings, which are not numbers, they evaluate to ‘0’.
    This means: “asdf” + 1 = 1; 2+4 = 6; “asdf” + 0 = 0;

    Variables don’t have to be declared – and default to the empty string, which has the numerical value of ‘0’.

    The next awesomeness of awk is that it automatically splits the current input line into fields. The field separator can be specified, but defaults to whitespaces. The single fields can be accessed by $1, $2, … $NF, i.e. NF is the number of fields. $0 is the contents of the full input line.

    And there you have it: you look over all ‘fields’ of the current line. The numerical values of all fields (which are 0 for strings) are accumulated in the variable s. After reading everything (END), the sum is printed.

    EDIT: this might conveniently work, but does not really answer the question, because it does not consider ‘number’ – sorry.

    A fix:

    awk '{ for (i = 1; i <= NF; i++) if ($i == "number") {s = s+$(++i)} }; END { print s+0 }' test.txt
    

    that way, it also results in 141 for input like:

    10 A dog has a ball number 49 number 34 number A
    Cats 1000 number 58

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file containing words separated by newline , like the following
I have some code reading a text file and scanning for words between brackets:
I have a simple text dictionary file, which contains words, separated by ';'.My problem
I have a text file which contains a long list of words. Some of
I have a text file with spam words. I want to have an array
I have a text file that looks similar to; section header 1: some words
I have a text file of 1000 words that I want to format an
I have a large text file with words that are interspersed with numbers and
I have a plain text file with words, which are separated by comma, for
I have a text file and I want to remove some lines that contain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.