LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-02-2006, 05:02 PM   #1
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
Need help with "read" command


I have a file named /etc/values, with the following 3 lines:
Code:
Gonzales, Juan;California;June
Johnson, Paul;Wyoming;April
Smith, John;Wisconsin;February
I'm trying to figure out how to use the read command and need a little advice. I'd like run a script that prints out:

Code:
Name:  Gonzales, Juan
State:  California
Month:  June

Name: Johnson, Paul
State: Wyoming
Month: April

Name: Smith, John
State:  Wisconsin
Month:  February
Here is my script so far:
Code:
#!/bin/sh
## Set IFS variable to semicolon as delimiter
IFS=;
for i in `cat /etc/values`; do
 echo $i | read -a field
 echo -e "Name:\t${field[0]}"
 echo -e "State:\t${field[1]}"
 echo -e "Month:\t${field[2]}"
done
But it's not working.
The output I am getting is:
Code:
Name:
State:
Month
:

I have read "man bash" and have tried many variations, but still not working right. If you have any tips, or see what I am doing wrong, feel free to point it out.

NOTE: The actual file I'll be using will have hundreds of records, delimited by a semicolon. Within the fields, there will be spaces and commas.

Last edited by WindowBreaker; 01-02-2006 at 05:04 PM.
 
Old 01-02-2006, 06:39 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
I'd use awk to do this, as it's perfect for this -- the following ought to work:

Code:
awk -F';' '{print "Name: ", $1, "\nState: ", $2, "\nMonth: ", $3, "\n\n"}' < /etc/values
I'm not totally sure how to do it in bash -- if I had to, I'd probably use read and then cut to split the line into its fields. IMO awk makes it much easier.
 
  


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
Getting "Short read in buffer_copy" error with "dpkg" stalefries Linux - Software 6 03-24-2010 03:19 PM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
cdrecord command-line parentheses "(" ")" coolingtower Linux - Software 1 10-28-2005 11:49 AM
Tiny Sofa 2.0 - I thought "halt", "reboot" were only root command ?? sorcerer Linux - Distributions 1 08-21-2004 03:28 PM
"segmentation error" when issuing "useradd" command through terminal with RH 9.0 kaihuang Linux - General 0 10-21-2003 11:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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