Click to see image I am trying to use the sed
command to extract a string in parentheses from many strings that a terminal returns. For example, I want to extract the string "Glass" that is in parentheses:
In Terminal I execute a program that prints out:
People: Leela, Amy, Bender, Zack
Item: Ship, Food, Gum, Water
Delivery: Pizza, Newspaper, Plant
Broken: Gas Tank, Watch, (Glasses)
For testing, purposes I wrote this in the terminal:
echo "Broken: Gas tank, Watch, (Glasses)" | sed 's/.*(\(.*\)).*/\1/'
The terminal returned:
Glass
which is the correct answer. But When I try to declare it in the .c file, my IDE seems it cannot interpreter the sed
command. It seems like I need to format the sed
command. The way I am using the sed
command is:
static const * getBrokenItem(void){
#define CMD " grep \"Broken: \" |sed 's/.*(\(.*\)).*/\1/'"
FILE *fp = NULL;
fp = open(CMD, "r");
bla..bla...
bla..bla...
}
The IDE (Visual Studio) does not like the "(" and ")" in
sed 's/.*(\(.*\)).*/\1/'"
I attached a snip here so it is easier to identify which "\(" and "\)" I'm referring to.
I have tried to add an additional "" next to the original "" since in C programming you need to do print("Backslash \") to print one backslash, but it did not work.
Can someone let me know what might be wrong please?