LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 10-18-2010, 11:31 PM   #1
OrangeGrover
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Rep: Reputation: 0
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??

Last edited by OrangeGrover; 10-18-2010 at 11:55 PM. Reason: adding code tags
 
Old 10-18-2010, 11:37 PM   #2
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
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
 
Old 10-18-2010, 11:57 PM   #3
OrangeGrover
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Okay, I re-posted it with the tags.
 
Old 10-18-2010, 11:59 PM   #4
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
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
 
Old 10-19-2010, 12:19 AM   #5
OrangeGrover
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Original Poster
Rep: Reputation: 0
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?
 
Old 10-19-2010, 12:22 AM   #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
Quote:
Originally Posted by OrangeGrover View Post
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
 
Old 10-19-2010, 12:30 AM   #7
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
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
 
  


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
NFS Large File Copies Fail - Error writing to file: Input/output error deck- Linux - Server 10 08-01-2022 02:30 AM
gcc will not execute, responds with 'no such file or directory' and 'no input file' nckeecho Ubuntu 9 07-24-2016 01:04 PM
data from input file to be taken and send to output file grishu Linux - Newbie 18 08-03-2010 08:02 AM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
Having rm or mv take file input BlueOrb Linux - Newbie 4 06-29-2004 09:42 PM

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

All times are GMT -5. The time now is 12:43 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