I would like to remove the double slashes at the beginning of a line to uncomment a line of code, the first commented for example. My approach works fine with a lot of other cases, but this time it isn't sufficient. Here is a piece of code from my file .sh:
while read line
do
if [[ $line == *"#define asdasd"* ]]
then
local prefix="\\/\\/ "
local new=${line#$prefix}
local sub="s/$line/$new/g"
echo "old line: " $line
echo "new line: " $new
echo $sub
perl -pi -e "$sub" $FILE
exit
fi
done < $FILE
EDIT:
My problem is more complex than this code. I want to uncomment (remove the double slashes) only one line of the lines that match the pattern and not necessarily the first one. It is the reason why I can't operate on a whole file, but line by line. So all the "perl on match" solutions doesn't work in my case.
EDIT:
I'm editing my question as suggested by some users.
- Background: I'm working with an embedded (complex) OS and I can't modify every files for my own ends (i.e. the
makefile
). - Purpose: I would like to build the code several times and every time with a different flag enabled.
In my current solution, I collect all the flags in a header file and I enable only one by one at every build. Here is an example of my .h file:
#ifndef SYSTEM_H
#define SYSTEM_H
#include <...>
#define BLA 1
#define BLABLA 2
#define BLABLABLA 3
// #define OPT_0 // Options disabled
// #define OPT_1
#define OPT_2 // Option enabled
// #define OPT_...
// #define OPT_...
// #define OPT_N
[...]
#define function(__exp) \
[...]
#endif
As you can see, I don't know how many options I have to enable/disable and there are other #define
instructions.
I'm writing a script that enable one flag at a time and build the project. Every time the compiled file is renamed to prevent the overwriting. A piece of my solution is based on the code listed above, but unfortunately the perl instruction doesn't modify the .h file removing the double slashes at the beginning of the selected line.