LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help getting input from a file. (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-getting-input-from-a-file-838958/)

OrangeGrover 10-18-2010 11:31 PM

Need help getting input from a file.
 
Hi everybody! I've been trying to figure this out for the past couple of days and I'm stumped...

I am trying to input from a file. The file's layout has three columns; a department number, a first name and a last name, each separated by a space, for example:

Code:

781-89-4271 Amy Balduff
839-98-4072 Brian Becker
846-43-4284 Christina Brendon
894-45-8694 Christine Cardosa-Ramirez
920-57-4251 Cindy Chen
469-55-6165 Corinna Deitz
466-54-3764 Cynth Dominguez
736-19-7345 Daniel Eversole
870-38-1858 David Flucker
539-71-2357 Dinno Gordillo
344-94-6979 Emily Hendrickson
532-37-4293 Gregory Herrera
916-86-6635 Heidi Hoang
614-90-6031 Hsu-Chia Houg
721-15-6603 Jack Jacobs
899-77-6019 Jamie Kim
671-12-5143 Jeffrey Koras
361-11-4712 Joseph Lewis
926-34-2055 Kurt Mananquil
885-66-4875 Leandra Martin
472-91-4771 Naomi Melick
663-25-1407 Omar Peralta
370-16-4687 Patricia Pesulima
207-82-3872 Robert Peters
743-92-4767 Rochelle Phang
962-92-4730 Salisha Salgado
587-95-1967 Savoeun Sanchez
758-12-8821 Shelley Siegel
479-65-3842 Stephanie Villarreal
644-86-6117 Thomas Williams

this is what I have so far:

Code:

#!/bin/bash
#
#adds user from a text format:

file=${1}

for i in $(cat $file);
do

      dept=$(echo $i | cut -d" " -f1)
      fname=$(echo $i | cut -d" " -f2)
      lname=$(echo $i | cut -d" " -f3)

      echo $fname $lname $dept
done

The output that I get is like this :

781-89-4271 781-89-4271 781-89-4271
Amy Amy Amy
Baldruff Baldruff Baldruff


instead of :
781-89-4271 Amy Baldruff

I need to input the words through separate variables because I will be passing them to other programs as soon as I can figure this out.

Any help??

Tinkster 10-18-2010 11:37 PM

Hi, welcome to LQ!

Can you post a bit of sample input via copy & paste?
And may I suggest that you use code-tags for both your
script & the data ...

Like [ code ] and [ /code ] , just w/o the spaces between
the brackets and code & /code respectively.


Cheers,
Tink

OrangeGrover 10-18-2010 11:57 PM

Okay, I re-posted it with the tags.

Tinkster 10-18-2010 11:59 PM

Ok, didn't see you edited your initial post
How's this?

Code:

#!/bin/bash
#
#adds user from a text format:

file=${1}

while read dept fname lname
do

  echo $fname $lname $dept
done < $file


OrangeGrover 10-19-2010 12:19 AM

Thank you! it works, and it's so much simpler.
One question though, what does the ' < $file ' part do on the last line?
Is it just part of the syntax of a while loop?

Tinkster 10-19-2010 12:22 AM

Quote:

Originally Posted by OrangeGrover (Post 4132092)
Thank you! it works, and it's so much simpler.
One question though, what does the ' < $file ' part do on the last line?
Is it just part of the syntax of a while loop?

Just telling bash to read from the file. And yes, it's
feeding the read in the while loop.


Cheers,
Tink

Tinkster 10-19-2010 12:30 AM

And for the record: to make your version work all it would have taken
was to introduce a new IFS ... I just thought the more elegant way
was preferable.
Code:

#!/bin/bash
#
#adds user from a text format:

file=${1}
IFS=$'\n'
for i in $(cat $file)
do
  dept=$(echo -n $i | cut -d" " -f1)
  fname=$(echo -n $i | cut -d" " -f2)
  lname=$(echo -n $i | cut -d" " -f3)

  echo $fname $lname $dept
done

Still quite verbose by comparison, but it works.


Cheers,
Tink


All times are GMT -5. The time now is 12:13 AM.