- sed (stream editor) is a Unix utility that parses text.
- Note: "sed -i" overwrites the original file with a new one.
- -e option indicates that the sed expression follows:
sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName
|
- The s stands for substitute. The g stands for global.
- The d command to delete lines that are either blank or only contain spaces:
$ sed -e '/^ *$/d' p
33
33
33
|
Regular expression metacharacters:
*The caret (^) matches the beginning of the line.
*The dollar sign ($) matches the end of the line.
*The asterisk (*) matches zero or more occurrences of the previous character.
- To delete any line containing the word "yourword" from the file "yourfile":
sed '/yourword/d' yourfile
|
- To delete all instances of the word "yourword":
sed 's/yourword//g' yourfile
|
- To delete two words from a file simultaneously:
sed -e 's/firstword//g' -e 's/secondword//g' yourfile
|
or
sed 's/firstword//g;s/secondword//g' yourfile
|