if (early_timeout == TRUE) {
    sprintf(send_buff, "XYZ: Command timed out",
    result = UNKNOWN;

Above is the sample content of file with proper indentation. Having a bit difficulty when trying to replace XYZ with ABC using sed,

  1. When using sed -i '/^.*sprintf/ s/XYZ/ABC/g' file.txt But this results in taking (printf, asprintf) as comparing word and replacing every occurance of line starting with (sprintf,asprintf, etc) throught the file, though I just need to replace line containing sprintf.
  2. When used sed -i '/^.sprintf/ s/XYZ/ABC/g' file.txt to compare the first word of line in this case i.e sprintf to compare and replace, but it is failing in case of extra indentation in the file.

Is there any way to particularly compare for word starting in line anywhere and then sed to replace.

One way I come across is, to align contents of my file.txt to left side on std output using sed -r 's/^[ \t]*//' and then apply sed -i '/^.sprintf/ s/XYZ/ABC/g' file.txt, Is there any way to combine and run in one go or any other way to achieve this? This seems to be not working as well and I'm a bit lost.

1 Answer 1

sed '/^[[:blank:]]*sprintf/ {s/XYZ/ABC/g;}' infile

this replace all XYZ string with ABC where the lines starts with none-or-more leading Space and Tabs followed by sprintf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.