UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 29th, 2007, 10:49 AM
bdg8255 bdg8255 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 3 bdg8255 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 10 sec
Reputation Power: 0
How to parse file and assign parsed contents to variables?

.ksh newbie

Could some please point me to some good help tools on how I can read in a file, and parse out content to assign to variables?

Contents of Example file (misc_data.dat):
warehouse,"/this/that"
home,"/this2/that"
node,"good"

Basically, i want to read in the file and assign "this/that", "this2/that", and "good" to different variables. Is this possible?

Last edited by bdg8255 : May 29th, 2007 at 10:56 AM. Reason: revise

Reply With Quote
  #2  
Old May 29th, 2007, 11:46 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 813 SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 6 h 38 m 32 sec
Reputation Power: 344
Yes, very easily!

Try:
Code:
awk -F\, '{print $2}' your_input_file


When you are happy it is producing what you want, then:
Code:
myvar=$(awk -F\, '{print $2}' your_input_file)

That will, of course, put ALL the output into one variable.
If you know what the 'first bit' of theline is going to be you can do
Code:
mywharehouse=$(grep "^warehouse" your_input_file |awk -F\, '{print $2}')

or something similar
__________________
"I feel so miserable without you; it's almost like having you here" - Stephen Bishop

Reply With Quote
  #3  
Old May 29th, 2007, 12:01 PM
ghostdog74 ghostdog74 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 104 ghostdog74 User rank is Lance Corporal (50 - 100 Reputation Level)ghostdog74 User rank is Lance Corporal (50 - 100 Reputation Level)ghostdog74 User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 21 h 15 m 52 sec
Reputation Power: 3
in bash
Code:
#!/bin/sh
IFS=,
while read var1 var2
do
echo "$var1 , $var2" 
done < "file"

Reply With Quote
  #4  
Old May 29th, 2007, 12:24 PM
bdg8255 bdg8255 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 3 bdg8255 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 37 m 10 sec
Reputation Power: 0
Thank you so much!

I really appreciate the help.

Reply With Quote
  #5  
Old May 30th, 2007, 11:07 PM
vgersh99 vgersh99 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 47 vgersh99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 5 h 51 m 28 sec
Reputation Power: 4
Send a message via AIM to vgersh99 Send a message via MSN to vgersh99 Send a message via Yahoo to vgersh99
Quote:
Originally Posted by ghostdog74
in bash
Code:
#!/bin/sh
IFS=,
while read var1 var2
do
echo "$var1 , $var2" 
done < "file"

Code:
#!/bin/ksh

while IFS=, read var1 var2
do
echo "$var1 , $var2" 
done < "file"

Reply With Quote
  #6  
Old June 20th, 2007, 02:42 PM
jdramire jdramire is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 2 jdramire User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 32 sec
Reputation Power: 0
I saw this thread from a search engine and registered on this website JUST because this solved a problem that I had been having for 3 days now, Thank you thank you thank you

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > How to parse file and assign parsed contents to variables?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT