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 07-23-2007, 09:07 AM   #1
SJD
LQ Newbie
 
Registered: Jan 2007
Posts: 25

Rep: Reputation: 15
Pulling variables from file with bash


Hi all,

I have a line that I have grep on which contains a number of variables and I would like to remove those numbers as variables. The line is:

XXX heads, YYY cylinders, ZZZ sectors

where XXX, YYY and ZZZ are numerical values that I need. The numbers could be any length so I cannot output a set number of characters. Is it possible to output these numbers as variables using bash? I apologise if this is a basic question, but I have looked on the internet, but I haven't been able to find anything to help me.

Thanks,

Steve
 
Old 07-23-2007, 09:17 AM   #2
Matir
LQ Guru
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507

Rep: Reputation: 128Reputation: 128
You could use awk to split out the fields, like this:
Code:
LINE="123 heads, 456 cylinders, 789 sectors"
HEADS=`echo $LINE | awk '{print $1}'`
echo "HEADS=$HEADS"
CYLINDERS=`echo $LINE | awk '{print $3}'`
echo "CYLINDERS=$CYLINDERS"
...
 
Old 07-23-2007, 10:20 AM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Not especially important, but maybe worth mentioning: to cut three awk invocations to one, you could use something like
Code:
LINE="123 heads, 456 cylinders, 789 sectors"
export $(echo $LINE | awk '{ print "HEADS="$1 " CYLINDERS="$3 " SECTORS="$5 }')
-- I'm having a brain-damaged day. It occurred to me to go entirely awk-less, but there's got to be a better way than this:
Code:
LINE="123 heads, 456 cylinders, 789 sectors"
read HEADS foo CYLINDERS bar SECTORS baz < <(echo $LINE)
echo "HEADS=$HEADS"
echo "CYLINDERS=$CYLINDERS"
echo "SECTORS=$SECTORS"
or this
Code:
LINE="123 heads, 456 cylinders, 789 sectors"
read -a DISKGEOM < <(echo $LINE)
echo "HEADS=${DISKGEOM[0]}"
echo "CYLINDERS=${DISKGEOM[2]}"
echo "SECTORS=${DISKGEOM[4]}"
but whatever. *sigh*

Last edited by slakmagik; 07-23-2007 at 11:08 AM.
 
Old 07-23-2007, 12:58 PM   #4
SeRGeiSarov
Member
 
Registered: Jan 2007
Location: Russia, Nizhniy Novgorod Region, Sarov town
Distribution: Mandriva 2007, Fedore Core 6
Posts: 52

Rep: Reputation: 15
Quote:
Not especially important, but maybe worth mentioning: to cut three awk invocations to one, you could use something like
Code:
LINE="123 heads, 456 cylinders, 789 sectors"
export $(echo $LINE | awk '{ print "HEADS="$1 " CYLINDERS="$3 " SECTORS="$5 }')
It is effective example.
How to do example without EXPORT?

Last edited by SeRGeiSarov; 07-23-2007 at 01:01 PM.
 
Old 07-23-2007, 01:28 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
I like the 'read' way, but here's one w/o any external binaries. Not *that* portable but oh well..
Code:
LINE="123 heads, 456 cylinders, 789 sectors"
LINE=${LINE//heads,}; LINE=${LINE//cylinders,}; LINE=${LINE//sectors}
LINE=(${LINE})
export `eval echo HEADS=${LINE[0]}`
export `eval echo CYLINDERS=${LINE[1]}`
export `eval echo SECTORS=${LINE[2]}`
 
Old 07-23-2007, 03:03 PM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by SeRGeiSarov
It is effective example.
How to do example without EXPORT?
One way would be to s/export/eval/ - as if we need more ways.
 
  


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
Read variables from a text file in Bash jakev383 Linux - General 5 12-20-2006 07:29 AM
howto pick bash variables from file imagineers7 Linux - Newbie 3 05-08-2006 01:56 AM
BASH script – reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM
Setting Bash Variables from outside data file llewis Programming 10 01-12-2005 04:30 AM

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

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