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"
fprintf(stderr,"Acccesing alloc id %x",$line);' < $line1 > $line1.swp
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
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.
|All times are GMT -5. The time now is 11:20 PM.|