For replacing everything before a keyword piece of code below works well.sed 's/.*keyword/string-to-be-replaced/'But for replacing n characters before the keyword including the keyword I couldn't find a solution. Anyone got an idea?
Using sed to replace n characters before a keyword including the keyword?
sed
You just need n . dots.n=[num]sed s/.\{$n\}keyword/REPLACE/...where [num] is meant to represent a positive integer should work.Note that is different than .*keyword in at least a couple of important respects. .* applies to any/all characters that occur before the last occurrence of keyword in sed's pattern space, and so if there are multiple keywords matched at once, .* will eat all but the last of them..\{[num]\}keyword will match only the first occurrence of the pattern which might be found in pattern space..\{[num]\} will require at least [num]-count chars to occur before keyword to qualify a match, whereas .* can match keyword even if it is found at the head of a line.An expression which more closely resembles this behavior in that it will match up to [num] chars occurring before keyword is...sed s/.\{0,$n\}keyword/REPLACE/