LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-29-2010, 12:31 PM   #1
adriumroot
LQ Newbie
 
Registered: May 2010
Posts: 3

Rep: Reputation: 0
Smile 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.
 
Old 05-29-2010, 12:40 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
http://mywiki.wooledge.org/BashFAQ/001
http://mywiki.wooledge.org/BashFAQ/073

http://mywiki.wooledge.org/BashGuide

Please use [code] tags.
 
Old 05-29-2010, 12:42 PM   #3
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
Line by line can be done with while read:

Code:
cat filename | while read aline; do
    echo $aline
done
 
Old 05-29-2010, 12:53 PM   #4
adriumroot
LQ Newbie
 
Registered: May 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you guys

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

Thank you very much
 
Old 05-29-2010, 01:01 PM   #5
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 197Reputation: 197
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.
 
Old 05-29-2010, 01:09 PM   #6
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 197Reputation: 197
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
 
Old 05-29-2010, 01:23 PM   #7
adriumroot
LQ Newbie
 
Registered: May 2010
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 05-29-2010, 11:35 PM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,492

Rep: Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892Reputation: 1892
M2C ... have a look at awk as well
 
Old 05-30-2010, 01:55 AM   #9
Simon Bridge
Guru
 
Registered: Oct 2003
Location: Waiheke NZ
Distribution: Ubuntu
Posts: 9,211

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by adriumroot View Post
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.
 
  


Reply

Tags
shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change first char of each line in a file to '0' using /bin/sh Xa! Programming 4 12-12-2010 11:51 PM
Error during reading a file line by line in C gozlemci Programming 2 03-06-2010 02:20 AM
extending the num of char capacity in a line in terminal/vector question Clarislock Programming 1 10-08-2009 05:32 PM
[Perl] chop last char from line noir911 Programming 8 02-20-2007 10:16 PM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM


All times are GMT -5. The time now is 03:24 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration