LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 06-11-2012, 08:26 AM   #1
linux_
LQ Newbie
 
Registered: Mar 2010
Location: Pune, India
Distribution: Fedora, CentOS, Ubuntu
Posts: 24

Rep: Reputation: 1
setting value of variable in text file


I have a text file which has content like
var1=abcd
var2=efgh
.
.
.
var10=mnop

I want to set the values of these variables from script
With sed it could do it. But, is there any simpler way?
 
Old 06-11-2012, 08:44 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
what do you mean by simpler way?
the most simple way is to generate this file in the script.
 
Old 06-11-2012, 11:52 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Code:
sed -r -i 's/var1=[[:alnum:]]+/var1=xyz/' filename
"wherever filename includes "var1=" and at least one letter or number, replace with "var1=xyx""

Seems pretty simple, but I won't promise that it is the absolute simplest....
 
Old 06-11-2012, 04:07 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I think I'd make it even simpler, and just replace the entire line:

Code:
sed -i  '/^var1/c var1=xyz' infile.txt
 
Old 06-11-2012, 04:28 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, I'm going to get all pedantic here, just to clarify the question.
The OP has a text file with content that looks like a bunch of assignments of values to variables. However, the 'variables' are not really variables until some script that can use that syntax executes it. So the real question is either how to craft a file with the specified content, or how to instantiate the assignments specified in the existing file. There really isn't enough information provided to unambiguously answer the question in either form.

If the OP wants to know how to create a file of the given format, he/she needs to tell us a bit about where the content is supposed to originate.
If the OP wants to instantiate the variables as they are defined in the file, we need to know in what language they are to exist. If we assume they are to become bash variables, then the easy way is to source the file:
Code:
. file.txt
After having done this, the variables will exist with the specified values in that shell. As they are not 'exported', child shells and other processes will not inherit them.

--- rod.
 
Old 06-11-2012, 07:47 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
I think the OP is basically looking at a cfg/data file that can be automatically updated by a feeder script, before being used by another script/program ??
Something like
Code:
feeder.sh updates file.txt

prog reads file.txt and processes

feeder is in loop, repeat from top
Almost like ETL: read DB1 then store results; 2nd prog reads store and eg inserts into DB2
 
Old 06-11-2012, 08:40 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Reading between the lines, I would guess that's probably what the OP means. That's still a lot of uncertainty. Still, how are we to have any inkling where the variables and respective values are to come from. Surely the data must exist, somewhere, in some form. We need to know more about where and what it looks like.

--- rod.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need help!!How to extract text in one file as variable then count it in another file? nobtiba Programming 14 01-04-2011 02:37 AM
Cannot read text from text file and store it in a variable using shell script anurupr Linux - Newbie 2 03-03-2010 02:38 PM
variable from text file bash drumbum Programming 1 07-30-2009 10:50 AM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 02:15 PM
Getting a variable from a text-file (bash) PatrickBecks Programming 4 02-14-2008 09:52 PM


All times are GMT -5. The time now is 04:46 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration