Monday, January 19, 2009

Some basic SED commands

'sed' is the search replace command that can be used in Linux.

Here are some examples on how to use 'sed':


sed 's/ //g'
- remove the spaces from the file. The 'g' indicates that the spaces should be replaced globally.

sed 's/cat/dog/g' - replace all instances of the word 'cat' with the word 'dog' in the file.

sed 's/2008-...../&,/g' - search for '2008-' followed by any 5 characters and append a ',' to the end of the string. The '&' indicates what was searched for which in this case was '2008-.....'.

All of the above examples can be used to alter a file and written to a new file e.g. cat filename | sed 's/2008/2007/g' > new_filename

No comments:

Post a Comment