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 198119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:54:40+00:00 2026-05-11T16:54:40+00:00

i have contents in a file like asdfb … 1 adfsdf … 2 sdfdf

  • 0

i have contents in a file

like

asdfb ... 1
adfsdf ... 2
sdfdf ..   3

I want to write a unix command that should be able to add 1 + 2 + 3 and give the result as 6

From what I am aware grep and awk would be handy, any pointers would help.

  • 1 1 Answer
  • 3 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-05-11T16:54:40+00:00Added an answer on May 11, 2026 at 4:54 pm

    I believe the following is what you’re looking for. It will sum up the last field in each record for the data that is read from stdin.

    awk '{ sum += $NF } END { print sum }' < file.txt
    

    Some things to note:

    1. With awk you don’t need to declare variables, they are willed into existence by assigning values to them.
    2. The variable NF is the number of fields in the current record. By prepending it with a $ we are treating its value as a variable. At least this is how it appears to work anyway 🙂
    3. The END { } block is only once all records have been processed by the other blocks.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a byte[] with the contents of file. I would like to send
I have a file called 'removethese' who's contents looks like this: ./test_dir/file1 ./test_dir/file2 ./test_dir/file
I have a bash script that sources contents from another file. The contents of
I have a database column that contains the contents of a file. I'm converting
I have sth like that inside *.txt file. function_name({one: {id: id_for_one, value: value_for_one}, ...});
I have a file contents of which looks like this 123,1,ABC,DEF 123,1,ABC 345,4,TZY 456,3,XYZ
I have a PHP code that stores contents of a url by file() method
I have created a UIView object using the contents of a NIB file like
I have a file in php that downloads a file and extracts it contents.
I have a data file in INI file like format that needs to be

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.