Problem parsing input with awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    I want add a line.For example:-
    123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25
    its answer should be like this 66
    i mean answer is total of 123456 line.
    How to do it?

  2. Relevant commands, code, scripts, algorithms:
    i am using Bourne shall

  3. The attempts at a solution (include all code and scripts):
    i am using this code but its not working it display
    123456
    i don't know

    awk '{ sum += $1 } END { print sum }' abc
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Humber College,Toronto,ON,Canada,Mohammod,ITS-510

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Try this...

echo "123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25" | awk '{for(i=2;i<=NF;i++){sum+=$i}print sum}'

#a crude way
echo "123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25" | sed 's/^[0-9]* //g;s/ /+/g' | bc

--ahamed

thank you so much........:slight_smile:

it works

---------- Post updated 11-08-11 at 04:37 PM ---------- Previous update was 11-07-11 at 10:44 PM ----------

But I want to take input from abc file and its output display in another file.I mean i have abc file which have:
123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25

its output display in another file:
66.

How to do it?

awk '{for(i=2;i<=NF;i++){sum+=$i}print sum}' input_file

sed 's/^[0-9]* //g;s/ /+/g' input_file | bc

--ahamed

Thanks sir.

I have a another problem.I have three-four files like this

COURSE NAME: Operating Systems
CREDITS: 4
123456 1 1 0 1 1 0 1 0 0 0 1 5 8 0 12 10 25
243567 0 1 1 0 1 1 0 1 0 0 0 7 9 12 15 17 15

Now i want to move student id and total marks from another file.like this......

Student# Operating Systems JAVA C++ Web Programming GPA
123456 76 63 50 82 67.75
243567 80 - 34 63 59

Can i use a read file or sort file. i tried to use read Command but just read print the file .How to print particular line....?