I'm looking to replace lines in a file. i.e. if "oldtext" is in the file at the start of a line, replace the whole line by "newtext".
Start of the line can be ^oldtext or oldtext will be indented by some whitespace/tabs
grep "^[\S]oldtext" myfile.txt # doesn't work
I need to calculate the same indentation level that "oldtext" had
prefixspaces = <number of spaces before the "mytext", converting tabs to 4 spaces>
I realise that sed is maybe the best way to achieve this, but I somehow need to calculate the prefix space first, then my output is something like:
sed -i -e 's/^$prefixspaces$newtext*$/$prefixspaces$newtext/g' myfile.txt
Can't work out how to calculate this. Maybe the $prefixspace can be held in a group () or something so that sed can easily calculate and work with it? Possibly this is a sed or awk one-liner, but I can't work it out after some searching.