ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm having some troubles with trying to use a variable within the sed command in a BASH Script...
Here is essentially what I'm trying to do:
choice="c1 c2 c3 c4"
for i in $choice
do
cat myfile | sed -e '$is/OFF/ ON/' > tmpfile
cp tmpfile myfile
done
**NOTE: The value contained in $i is the line number sed needs to perform the substitute on...
Basically, i'm trying to do sed -d '1s/OFF/ ON/' , but have the 1 replaced with the dynamic variable that sets the line number.
I've tried quite a few ways now, I'm sure I'm just a syntax thing, I've tried double quoting, single quoting, a combo of the two, parathesises, but am not having any luck here.
a=1
sed "s/$a/new/" filename replaces the value of a with new
sed 's/$a/new/ filename replaces the literal "$a" with new
sed '$as/old/new/' filename at the end of the file, append the string "s/old/new"
sed "$as/old/new/" filename results in an error
sed ""$a"s/old/new/" filename WORKS!!!! (But why?)
Also---in the OP, the loop gives values of c1, c2, c3....etc. These won't work in the sed expression---eg what is "sed 'c1s/old/new/' filename" going to do?
The values of each choice are my selections from a `dialog` checklist that I need to run a function for. The function is to update an options_table that turns a value ON or OFF.
The checklist has 25 choices, The values I grab are the quoted line numbers (eg. "1" , "2" , etc...) that I need to edit.
then, I actually run the for loop to update the values in an options_table:
for i in $choice
do
c=`echo $i | sed /\"/s/\"//g`
sed ""$c"s/OFF/ ON/" $TABLE
done
}
If this helps. The sed command you posted, allows me to update the values as it displays on the screen, but it does NOT actually change the file permanently, just the screen output is modified. I can get around this by sending output to a tmpfile, then copying it back to the original file, but was hoping there is a way I can this in one line for efficiency purposes...
Just to note, the reason it only updates values from OFF to ON is because I copy table_template to the original with all OFF values, and based on the selections that are chosen, I rebuild the file from there, updating values to ON by running a for loop on my chosen selections.
I apologize if any of my wording doesn't sound quite right, I'm still learning BASH and am trying to explain things as best as I can.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.