LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Programing Question: Reading file line by line.. then char by char in each line (http://www.linuxquestions.org/questions/programming-9/programing-question-reading-file-line-by-line-then-char-by-char-in-each-line-810946/)

adriumroot 05-29-2010 12:31 PM

Programing Question: Reading file line by line.. then char by char in each line
 
Hello.

I would like to say that I've never programed shell scripting, and I'm asking this because I want to help a friend with a homework XD but I really don't know if what I'm doing is right or wrong nor do I have linux to test hehehe.... sorry guys, bare with me please.

Code goes like so:
#!/bin/bash
line=0
sum=0
for var in `wc -l data.txt | awk '{print $1}'`;do
char=0
while[$char -lt ${#var}];do
$sum=$sum+${var[$char]}
let char=char+1 //or $char=$char+1 ?
done
done
echo "Result is:$sum"

I simply want to read a file "data.txt" line by line
Then char by char and add them into a result var.
The file is supossed to always contain numeric values

Thanks.

tuxdev 05-29-2010 12:40 PM

http://mywiki.wooledge.org/BashFAQ/001
http://mywiki.wooledge.org/BashFAQ/073

http://mywiki.wooledge.org/BashGuide

Please use [code] tags.

eantoranz 05-29-2010 12:42 PM

Line by line can be done with while read:

Code:

cat filename | while read aline; do
    echo $aline
done


adriumroot 05-29-2010 12:53 PM

Thank you guys

I'll do what I can with the help you provided.

Thank you very much

Simon Bridge 05-29-2010 01:01 PM

Quote:

bare with me
While I can barely bear your bareness, I refuse to be bare with you :) just have to grin and bear it.
I guess I could bare a bear if you supply the clippers... but let us take arms against this sea of berings and, by opposing, end them!

Quote:

I simply want to read a file "data.txt" line by line
Then char by char and add them into a result var.
The file is supossed to always contain numeric values
The secret with homework is that they are exercises about a lesson just gone. The answer is in your friends notes - just not in so many words. So your first step is to reword the task so it reads more generically - thus:

you want to sum the numbers in a data file. In the first instance, the numbers are seperated by newline characters and in the second they are all single digit numbers, and you don't care about other the newlines.

This is an exercise in converting between string and numerical data as well as reading from a file.
Have a look at other examples:
http://www.linuxquestions.org/questi...script-191207/
http://ubuntuforums.org/showthread.php?t=707356
... yours is just like that only the formatting is different. Show it to your freind and try help him figure it out.

When you are helping someone with their homework, it is best to let them do the actual discovery - you can help best by guiding them through the search process.

Simon Bridge 05-29-2010 01:09 PM

Ah - forgot:

Presumably for one character at a time

cat filename | while read -n 1 achar; do
echo $achar
done

presumably the lesson on the read command just finished.
So you'd help your freind to read the man page and the notes :)

adriumroot 05-29-2010 01:23 PM

Hahaha thanks Simon.

Yes I now that the point of homework is to learn. And I'm not telling my friend the answers since I don't know them myself.
If it is any good... I'm learning too. My programming skill are pretty low but I guess everyone is like that at some point =P.

Thanks man

grail 05-29-2010 11:35 PM

M2C ... have a look at awk as well

Simon Bridge 05-30-2010 01:55 AM

Quote:

Originally Posted by adriumroot (Post 3985626)
Hahaha thanks Simon.

Yes I now that the point of homework is to learn. And I'm not telling my friend the answers since I don't know them myself.

No worries - the important skill is learning how to figure stuff out rather than just memorizing a bunch of tricks. This is a common trap for people new to computers and programminrg - but also to people not used to tutoring: we end up providing walk-throughs and hoping that the person tutored just gets it.

One of my jobs is providing professional development to secondary and tertiary educators - making them more effective teachers. So when I see someone asking for help with tutoring I like to push for better methods - can't hurt right?

The main lesson I wanted you to get here was to stand back and look around the problem rather than right at it. How can you most benefit your friend?

Usually the tutors main advantage is an improved ability to figure stuff out - look at how you go about it and guide your friend through the same method. This will also benefit you, because the self-observation required will show you stuff you didn't realise. Give it a go.

Quote:

If it is any good... I'm learning too.
Way to go: never stop learning. The big deal in education research now is in information literacy, inquiry learning, and a buzz-phrase called "life long learning". NCLB has triggered a flood of decent research to base actual methods on - so the techniques are getting really solid where it used to be mostly guesswork. I'm not trying to turn you into a uber-teacher though, just point to a useful path for you to take that'll be really fast to learn.

Don't sweat it. Have fun.


All times are GMT -5. The time now is 11:31 PM.