I want to delete newlines after lines containing a keyword e.g. like modifiers private:,public: or protected: to fulfill our coding standard. I need a command line tool (Linux) for this, so please no Notepad++, Emacs, VS, or Vim solutions, if they require user interaction. So in other words I want to do a:
sed -i 's/private:\s*\n\s*\n/private:\n/g'
I’ve seen this question but was unable to extend it to my needs.
If I understand correctly, you want to remove empty lines which follow a line containing
private:,public:, orprotected:.Explanation:
:loopcreate a label/private:\|public:\|protected:/will search for lines containing the pattern.n;/^$/dwill load the next line (n), check whether it is an empty line (/^$/), and if it is, delete the line (d).Tloopbranch to labelloopif there was no match (line was not empty)I am no sed guru, there might be more elegant ways to do this. There might also be more elegant ways to do this in awk, perl, python, whatever.