Tuesday, October 11, 2011

Vim and Sed

A tutorial on things that you could do with sed on vim.
  1. Search and replace pattern
  • Replace one pattern in a given line

Take the cursor to the desired line, press ESC and then colon sign and fire the following command

:s/OLD/NEW

  • Replace all pattern in a given line

Take the cursor to the desired line, press ESC and then colon sign and fire the following command

:s/OLD/NEW/g

  • Replace all pattern withn a given range of lines

Press ESC, colon sign, give range of line and then sed command. LINE_START is start line while LINE_END is end line between which all occurances of OLD will be replaced by NEW

:LINE_START,LINE_END s/OLD/NEW/g

  • Replace all pattern in the file

Press ESC, colon, line range and then sed commnad. 1,$ indicated first line and last line of the file

:1,$ s/OLD/NEW/g