Problem parsing input with awk

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Problem parsing input with awk
# 1  
Old 11-07-2011
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
Code:
    awk '{ sum += $1 } END { print sum }' abc

4. 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).

Last edited by Arsh10; 11-08-2011 at 05:26 PM.. Reason: code tags
# 2  
Old 11-07-2011
Try this...
Code:
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
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 11-08-2011
Problem parsing input with awk

thank you so much........Smilie

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?



Last edited by Arsh10; 11-08-2011 at 05:43 PM.. Reason: i cannot use a meaning less subject
# 4  
Old 11-09-2011
Code:
awk '{for(i=2;i<=NF;i++){sum+=$i}print sum}' input_file

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

--ahamed
# 5  
Old 11-14-2011
Thanks sir.

I have a another problem.I have three-four files like this
Code:
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......
Code:
Student# Operating Systems JAVA C++ Web Development 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....?


Moderator's Comments:
Mod Comment Please use code tags <- click the link!

Last edited by zaxxon; 11-14-2011 at 10:10 AM.. Reason: code tags, see PM
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk parsing problem

Hello fellow unix geeks, I am having a small dilemna trying to parse a log file I have. Below is a sample of what it will look like: MY_TOKEN1(group) TOKEN(other)|SSID1 MY_TOKEN2(group, group2)|SSID2 What I need to do is only keep the MY_TOKEN pieces and where there are multiple... (7 Replies)
Discussion started by: dagamier
7 Replies

2. Shell Programming and Scripting

Complex text parsing with speed/performance problem (awk solution?)

I have 1.6 GB (and growing) of files with needed data between the 11th and 34th line (inclusive) of the second column of comma delimited files. There is also a lot of stray white space in the file that needs to be trimmed. They have DOS-like end of lines. I need to transpose the 11th through... (13 Replies)
Discussion started by: Michael Stora
13 Replies

3. Shell Programming and Scripting

Need help in parsing an input in perl

I am executing a command it is returning me something like this name ip port ------------------------------------ http-listener-1 * 6712 http-listener-2 * 8709 I have a subroutine getListenerName($porttobeChecked) This subroutine returns me the name of the listener if i pass a... (4 Replies)
Discussion started by: javaholics
4 Replies

4. Homework & Coursework Questions

Shell: Parsing Input

1. The problem statement, all variables and given/known data: I'm fairly confident I can brute force this assignment, but let's not do that ;-). Basically I'm required to support input such as ps aux | grep blah >> blah.txt& echo 'slslslsl' My question is what is the best way to parse that... (4 Replies)
Discussion started by: someoney3000
4 Replies

5. Shell Programming and Scripting

Help parsing job script input parameters

I have a job script that runs with input parms from the command line. job.sh -p parm1_parm2_parm3_parm4_file_1.dat The parms are separated by _ The last parm is a file name and can have an _ in the name. I currently use the following commands to extract the parms parm1=`eval echo... (3 Replies)
Discussion started by: jclanc8
3 Replies

6. Shell Programming and Scripting

Another parsing line awk or sed problem

Hi, After looking on different forums, I'm still in trouble to parse a parameters line received in KSH. $* is equal to "/AAA:111 /BBB:222 /CCC:333 /DDD:444" I would like to parse it and be able to access anyone from his name in my KSH after. like echo myArray => display 111 ... (1 Reply)
Discussion started by: RickTrader
1 Replies

7. UNIX for Dummies Questions & Answers

Parsing name and phone as input and then print sub and marks out

I have a file like this : name phone id sub marks abc 2345 45 mat 90 bgt 6573 54 eng 89 ... .... .. ... .. ... .... .. ... .. Now i need to take in name and phone as input and then print sub and marks out, can u give me a sample code for this. P.S. If there are two of with same... (2 Replies)
Discussion started by: SasankaBITS
2 Replies

8. Shell Programming and Scripting

parsing file2 with input from file1

Sorry dublication with previous thread... please delete it Hi all i need and appreciate your help creating a script in ksh for the following case Two files exists with questionmark delemeter: File1.txt: A;B;C;F;D;K; File2.txt A,name,address1; K,name,surname,phone; C,name,phone;... (1 Reply)
Discussion started by: forumsgr
1 Replies

9. Shell Programming and Scripting

awk parsing problem

I need help with a problem that I have not been able to figure out. I have a file that is about 650K lines. Records are seperated by blank lines, fields seperated by new lines. I was trying to make a report that would add up 2 fields and associate them with a CP. example output would be... (11 Replies)
Discussion started by: timj123
11 Replies

10. Shell Programming and Scripting

Parsing input paramter in a script

Hi folks I am having a little trouble in parsing a variable read into a ksh script I have a bunch of variables passed into script test.ksh HOST SERVER JOB1 JOB2 JOB3 JOB4 JOB5 What I want to do is read all the $JOB variables ($JOB1, $JOB2, $JOB3) into a variable and then read that variable... (2 Replies)
Discussion started by: Anubhav
2 Replies
Login or Register to Ask a Question