Modify the last line containing the same string using variable using sed in C

Viewed 55

I would like to modify the string values and extract the string values based on the similar strings.

I have a file with the following content:

 Item=Basket  
 Fruit=Orange  
 Fruit=Pears  
 Color=Black  
 Condition Good  
 Type=Riped

I want to write 2 functions: to get the last fruit in a variable and to change the last fruit with a variable. So, I have used the following:

I want to collect all the fruits in an array. But it returns the whole file without fruit= on the lines.

const char *last_fruit(void){
   FILE *file;
   const char* LFruit;
   popen(); //Don't know
   pclose();
   return(LFruit);
}

void change_last_fruit(const char* myfruit);
   FILE *file;
   file= popen("sed -i 's/.*fruit=.*/fruit=myfruit/' /myfolder/myfile.txt", "r");
   pclose(file);
   return();
}

int main(){
    const char* output = last_fruit();
    change_last_fruit(cherry);
    return;
    }

But the whole fruits are replaced by myfruit instead of the last line of fruit with Cherry.

Pleased to hear some suggestions.

0 Answers
Related