Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-14-2008, 06:26 AM   #1
LQ Newbie
Registered: Jan 2008
Posts: 27

Rep: Reputation: 15
Question variable substitution in sed


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

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


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


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

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.


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
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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration