LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2008, 11:26 PM   #1
stlouis
Member
 
Registered: Jul 2006
Location: Sault Ste. Marie, Ontario
Distribution: RedHat, CentOS, Fedora Core, Gentoo, Slackware
Posts: 63

Rep: Reputation: 16
SED in BASH Script - Syntax Problem


Hi,

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.


TIA


Jeff
 
Old 01-12-2008, 11:58 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
try using double quotes and you can do away with the cat. Use this syntax : sed <pattern> <file>
 
Old 01-13-2008, 10:00 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,803

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Hmmmmm.....

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?

Last edited by pixellany; 01-13-2008 at 10:01 AM.
 
Old 01-13-2008, 11:31 AM   #4
stlouis
Member
 
Registered: Jul 2006
Location: Sault Ste. Marie, Ontario
Distribution: RedHat, CentOS, Fedora Core, Gentoo, Slackware
Posts: 63

Original Poster
Rep: Reputation: 16
Actually, the c1, c2, c3, c4 are just names I used here for reference purpose.

c1 = choice 1
c2 = choice 2
c3 = choice 3
c4 = choice 4

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:

here is the function:

up_opts()
{
choice=`cat $tempfile`
cp -f $TABTPT $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.


Jeff
 
Old 01-13-2008, 11:38 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,803

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
To change a file "in place":
sed -i '...stuff...' filename

To write to a new file:
sed '...stuff...' filename > newfilename

Really good SED tutorial here: http://www.grymoire.com/Unix/Sed.html
 
  


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
Bash script syntax error snowman81 Programming 5 11-16-2007 03:35 AM
How to check the bash script syntax? mesh2005 Linux - General 2 04-23-2006 09:22 AM
BASH - simple script issue - syntax? tw1ggy5 Programming 11 05-21-2004 10:09 AM
problem with sed in a bash script nexus55 Linux - Software 6 05-03-2004 10:40 PM
help with basic syntax in bash script Supp0rtLinux Linux - Software 4 03-27-2003 07:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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