Using sed
, how would one insert text after a character that precedes (or follows) some string by N
occurrences. As an example, suppose that the line of text to be edited is the following:
command -some -args -c 'a quoted section;some;lines;of code;keyword;more lines;etc();'
After finding this line in a text file (perhaps through the unique string command
), I wish to insert text after the second (N=2) semicolon before keyword
(i.e., the semicolon separating lines
and of
). I would specifically like to use sed
for the purpose.
Continuing with this example, the expected output would be:
command -some -args -c 'a quoted section;some;lines;INSERTED_STRING;of code;keyword;more lines;etc();'
where INSERTED_STRING;
(provided to sed, e.g., via a shell variable) was inserted at the desired position.