LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-06-2003, 10:18 AM   #1
myketech
LQ Newbie
 
Registered: Mar 2003
Location: texas
Posts: 2

Rep: Reputation: 0
script question? read from a file


I have a shell script question. I have a data file that looks simliar to this:


user1 texas 78155 "John Lynn Smith" 830-555-4578
user2 florida 35698 "Shay Coleman" 597-555-1234
...
...
...
...and so on


i'm trying to write a script that will read in each user's information from this data file.
The problem I am having is storing the user's full name in one variable because of the spaces.

ex.
"John Lynn Smith" stored as fullname

I want to be able to store everything in between the quotes as one variable.


here's the section of the script I am using


...
...
while read username state zip fullname phonenumber

do

echo "$username $state $zip $fullname $phonenumber" >> file

done < datafile
...
...



I am only getting the first part of the name,
"John
stored in fullname, when I really want everything in between the quotes,
"John Lynn Smith"
stored in fullname.

What do I need to fix?
Thanks in advance!
 
Old 03-06-2003, 10:27 AM   #2
Shak
Member
 
Registered: May 2002
Location: Huddersfield
Distribution: Redhat (7.2, 7.3, 8.0), Debian, Slackware, Gentoo, FreeBSD
Posts: 169

Rep: Reputation: 30
You need to add a "\" before each space to make BASH ignore it, the same as you have to when using the shell.

Hope this helps,

Shak
 
Old 03-06-2003, 10:30 AM   #3
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Alternatively you can make the script read everything in the quotation marks as the username.
 
Old 03-06-2003, 10:36 AM   #4
myketech
LQ Newbie
 
Registered: Mar 2003
Location: texas
Posts: 2

Original Poster
Rep: Reputation: 0
I'd like to do this without editing the original data file....it is very long. Thanks!
 
Old 03-06-2003, 11:12 AM   #5
vladkrack
Member
 
Registered: Oct 2002
Location: Curitiba - Brazil
Distribution: Conectiva
Posts: 334

Rep: Reputation: 30
Then you can use an auxiliar var:

while read username state zip aux
do
fullname=`echo $aux | cut -d\" -f2`
phonenumber=`echo $aux |cut -d\" -f3`
.
.
done

It's not the best solution, but should do it ...
 
Old 03-06-2003, 07:58 PM   #6
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Re: script question? read from a file

Quote:
Originally posted by myketech I have a data file that looks simliar to this:

user1 texas 78155 "John Lynn Smith" 830-555-4578
user2 florida 35698 "Shay Coleman" 597-555-1234
...
...
...
...and so on

I want to be able to store everything in between the quotes as one variable.

Howzabout:
Code:
while read RECORD
do
   set $RECORD
   USERNAME=$1
   STATE=$2
   ZIP=$3
   FULLNAME=$4
   PHONENUMBER=$5
   echo "$USERNAME $STATE $ZIP \"$FULLNAME\" $PHONENUMBER >> file
done < datafile
(Did you need the full name inside double quotes in the output file? If not omit them from the echo line above)

Hope that helps,

Rick
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Shell] Read a File from script yussef Programming 4 08-19-2008 04:26 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM
Script to read a simple text file ravykanth Linux - Newbie 9 10-22-2003 04:15 AM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 05:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:33 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
Open Source Consulting | Domain Registration