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 - 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, 07: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, 07:44 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

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

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 03:07 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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, 03:28 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
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, 06:47 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 07:40 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
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



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 01:37 AM
Cannot read text from text file and store it in a variable using shell script anurupr Linux - Newbie 2 03-03-2010 01:38 PM
variable from text file bash drumbum Programming 1 07-30-2009 09:50 AM
How to get variable from text file into Bash variable mcdef Linux - Software 2 06-10-2009 01:15 PM
Getting a variable from a text-file (bash) PatrickBecks Programming 4 02-14-2008 08:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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