LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   loop thru a file (https://www.linuxquestions.org/questions/linux-newbie-8/loop-thru-a-file-4175460797/)

alexrusso 05-05-2013 11:25 AM

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:LastName:DOB

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

TB0ne 05-05-2013 11:50 AM

Quote:

Originally Posted by alexrusso (Post 4945421)
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:LastName:DOB

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/

David the H. 05-05-2013 04:31 PM

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.

alexrusso 05-05-2013 05:58 PM

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

chrism01 05-05-2013 06:05 PM

A good linux tutorial
http://rute.2038bug.com/index.html.gz

David the H. 05-07-2013 12:09 PM

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.


All times are GMT -5. The time now is 06:12 PM.