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
 
LinkBack Search this Thread
Old 07-14-2008, 06:26 AM   #1
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Rep: Reputation: 15
Question variable substitution in sed


Hi,

This is a simple stuff yet i couldn't solve it myself.
The issue goes as below,

i need to place fprintf after every pattern matched in a C code.
eg: if a c code has

Alloc(ALLOCID_2x0023);
then a printf should be placed after the line as below

fprintf (stderr,"Accessing alloc id %x", ALLOCID_2x0023);

there are list of files to be modified whose list is there in the "file_list". the pattern to be searched are there in "alloc_ids".


i wrote a code,

while read line
do
while read line1
do
grep $line $line1
if [ $? -eq 0 ]
then
##echo " The $line in file $line1"
sed '/Alloc(/a\
fprintf(stderr,"Acccesing alloc id %x",$line);' < $line1 > $line1.swp

fi


done <file_list
done <alloc_ids



The problem is, i'm not able to do variable substitution for the variable "$line" which has the pattern.

If i put quotes instead of single quote for the sed, it throws an error "cannot parse ...bla..bla.."

On the whole , it just writes the entire fprintf line as a string in the new file inclusive of $line being written as such without any substitution.



Any help would be appreciated..please


thanks,

~ gaynut
 
Old 07-14-2008, 07:38 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
The normal single quotes used with SED will not allow the shell to expand a variable name--eg "$var". To fix this, use double-quotes for SED. Then, if you need the (SED) special meaning of "$" (end of line), you would escape it with "\".

Keep in mind that--in many situations--quoting is not required at all. Many books say "use single-quotes to be on the safe side", but you have to then recognize when the single-quotes won't work.

Any kind of quoting is only required to prevent a process from interpreting one or more characters rather than passing it on the next process.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple substitution with sed? ocicat Programming 9 02-22-2008 11:45 PM
sed substitution with p flag 7stud Linux - Newbie 2 03-03-2007 04:15 AM
Command substitution and sed daYz Linux - General 9 11-04-2006 01:15 AM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 12:36 AM
sed substitution error BlkPoohba Programming 1 08-25-2004 02:00 PM


All times are GMT -5. The time now is 02:55 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration