LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 06-21-2011, 08:20 PM   #1
roololing
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Rep: Reputation: Disabled
Read Each Line of a File as Input


I'm writing a program which now accepts user input:
Code:
echo "Enter a date in the format YYYY MM DD hh mm ss."
read gregyr gregmo gregdy greghr gregmn gregsc
This lets the user input a date and time, such as 2011 06 21 15 12 45, and have each number assigned to their corresponding variable. Later in the program, these variables are put into an equation, and then the terminal spits out the answer.
Now I have to have the program read all of the lines from a text file, which is in this format, assign the variables, solve the equations, and put the solutions into a new file. I haven't been using bash for very long; any help would be appreciated.
 
Old 06-21-2011, 08:27 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Use a standard while read loop:
http://mywiki.wooledge.org/BashFAQ/001
 
1 members found this post helpful.
Old 06-21-2011, 08:28 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Something like this:
Code:
while read gregyr gregmo gregdy greghr gregmn gregsc; 
do
   #<your magic goes here>
done <infile


Cheers,
Tink
 
1 members found this post helpful.
Old 06-21-2011, 08:36 PM   #4
roololing
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
Something like this:
Code:
while read gregyr gregmo gregdy greghr gregmn gregsc; 
do
   #<your magic goes here>
done <infile


Cheers,
Tink
Thanks.

The program just repeatedly tells me that the Entry is invalid, which means that whatever it's getting isn't in the correct format...

Last edited by roololing; 06-21-2011 at 08:43 PM.
 
Old 06-21-2011, 09:44 PM   #5
roololing
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
OH.
I think the program is reading the newline as part of gregsc....
Is this fixable without having to put spaces after the thousands of lines in this file?
 
Old 06-21-2011, 10:08 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I'm not sure what your actual file format is, if it's a matter of
DOS vs Linux line endings, just use fromdos or dos2unix; if they're
not available, tr or sed could easily help....



Cheers,
Tink

Last edited by Tinkster; 06-21-2011 at 10:10 PM.
 
Old 06-22-2011, 03:15 PM   #7
roololing
LQ Newbie
 
Registered: Jun 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Okay I just did some find/replace magic and made spaces after every line, and those go into a junk variable.
Code:
while read gregyr gregmo gregdy greghr gregmn gregsc whatevz;
do
        #boopboopdeboopdoop
done < infile > outfile
So I'm finished! Thank you so much!
 
Old 06-22-2011, 03:33 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Nice :}

Glad you got it going!


Cheers,
Tink

Last edited by Tinkster; 06-22-2011 at 04:18 PM.
 
  


Reply



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
Scripting question - feed an input file into an if statement line-by-line kmkocot Linux - Newbie 10 01-18-2010 11:49 AM
how to read input line by line (awk) slcalice Programming 4 03-09-2009 02:12 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
read the input file from the specified line no till end suchi_s Programming 5 09-09-2004 04:36 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration