LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-05-2013, 12:25 PM   #1
alexrusso
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
loop thru a file


Hello,

Need help on the below, I am new to linux and let me know what i am doing wrong:

I have to read from a text file,
each line has 3 strings with delimiter ':'
example: alex:russo:1986 which is maps to Firstname:LastNameOB

i need something like below:

while IFS=':' read line
do fn=`echo $line
ln=`echo $line`
dob=`echo $line`
echo 'firstname='$fn
echo 'lastname='$ln
echo 'DOB='$dob
done > input.txt

my input.txt file looks like below:
alex:russo:1985
sam:dixy:1985
ronald:smith:1984
curry:leaves:1987
linex:redhat:1950

Please help
 
Old 05-05-2013, 12:50 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,321

Rep: Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470
Quote:
Originally Posted by alexrusso View Post
Hello,
Need help on the below, I am new to linux and let me know what i am doing wrong:

I have to read from a text file, each line has 3 strings with delimiter ':'
example: alex:russo:1986 which is maps to Firstname:LastNameOB

i need something like below:
Code:
while IFS=':' read line
do fn=`echo $line
ln=`echo $line`
dob=`echo $line`
echo 'firstname='$fn
echo 'lastname='$ln
echo 'DOB='$dob
done > input.txt
my input.txt file looks like below:
Code:
alex:russo:1985
sam:dixy:1985
ronald:smith:1984
curry:leaves:1987
linex:redhat:1950
Please help
We will be glad to help, but it's helpful to provide details, like what error(s) you get, and what you've done to try to solve the issue? This seems very much like a homework question, but a few things jump out immediately:
  • You're missing the #!/bin/bash at the top of your script
  • When executing the script you posted, you're missing the closing backtick on line 4.
  • You're not using the IFS correctly in the context you're after.
  • You're not reading the file with the >...you're outputting TO it. To read, you need the < operator.
There are lots of bash scripting tutorials and examples you can find on Google...that's always a good starting point. Try something like:
Code:
while IFS=: read fn ln dob
do
  echo -e "firstname : $fn\n\          
 lastname : $ln\n\          
 DOB :\t $dob\n\          
done < input.txt
Read up on the bash operators, the IFS function, and go through tutorials.
http://tldp.org/LDP/abs/html/
 
1 members found this post helpful.
Old 05-05-2013, 05:31 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
I will only say that you're doing just about everything wrong. You need to stop and do a good read of the BashGuide or another good scripting reference, because you really need get the basic concepts down.

One problem I see, for example (other than what was mentioned above) lies here:
Code:
while IFS=':' read line
The IFS setting tells read to split the line on that character and store it in multiple variables. But you only have one variable here, so what's it supposed to do?

And please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


PS: Useless Use Of Echo! Try var1=$var2 instead.

Last edited by David the H.; 05-05-2013 at 05:41 PM. Reason: additions and rewording
 
1 members found this post helpful.
Old 05-05-2013, 06:58 PM   #4
alexrusso
LQ Newbie
 
Registered: May 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Smile

Thanks David and TB0ne, as i said I am new to linux and surely love to going thru the BashGuide. TBOne your response did help me under stand how the IFS will work and split the line, so i should be writting some this like

Code:
while IFS=':' read var1 var2 var3 do
# my logic
done < input.txt
thanks for your responses guys, I was able to acheive what was required, i know need to learn a lot more.....
 
Old 05-05-2013, 07:05 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
A good linux tutorial
http://rute.2038bug.com/index.html.gz
 
Old 05-07-2013, 01:09 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981Reputation: 1981
That's a good attitude to have. Keep at it and you'll get there in time.

I was worried that I might've across as too harsh in my last post. But to be honest, there are an impressive number of errors in the small bit of code you posted. I didn't even mention that the quoting is all wrong.

Now that you've got it working, here's how it should probably look:

Code:
#!/bin/bash

infile=input.txt

while IFS=':' read -r firstname lastname dob; do

   echo "First name is: $firstname"
   echo "Last name is: $lastname"
   echo "Date of birth is: $dob"
   echo

done <"$infile"

exit 0
Notice here that it's a good idea to keep code and data as separate as possible. Don't hard-code your filenames, but instead configure them in variables at the top of the script.

Last edited by David the H.; 05-07-2013 at 01:11 PM.
 
1 members found this post helpful.
  


Reply


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 09:47 PM
loop file future2012 Linux - Newbie 2 07-17-2009 12:28 PM
Geting error loop QFike:getch: File not open QFile:atEnd: this file is not open badgerbox76 Linux - Newbie 6 01-07-2006 06:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:25 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration