Softwares ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Main /
SedStreamEditorSed stream editor $ cat -n sedexample 1 This is a test file. 2 You can create a test file using a vi editor 3 vi testfile 4 vi is a powerfull editor. p Print the current pattern space. $ sed '/test/p' sedexample This is a test file. This is a test file. You can create a test file using a vi editor You can create a test file using a vi editor vi testfile vi testfile vi is a powerfull editor. In the above example the lines containing the search string are printed twice. Sed editing commands Command Result a\ Append text below current line. c\ Change text in the current line with new text. d Delete text. i\ Insert text above current line. p Print text. r Read a file. s Search and replace text. w Write to a file. Sed options Option Effect -e SCRIPT Add the commands in SCRIPT to the set of commands to be run while processing the input. -f Add the commands contained in the file SCRIPT-FILE to the set of commands to be run while processing the input. -n Silent mode. -V Print version information and exit. $ sed -n '/test/p' sedexample This is a test file. You can create a test file using a vi editor vi testfile -n, --quiet, --silent suppress automatic printing of pattern space $ cat -n sedexample 1 This is a test file. 2 You can create a test file using a vi editor 3 vi test file 4 vi is a powerfull editor. See the lines not containing the search string, using the example below. $ sed '/test/d' sedexample vi is a powerfull editor. $ sed '1,3d' sedexample vi is a powerfull editor. To print the file starting from a certain line until the end of the file, use a command similar to this $ sed '3,$d' sedexample This is a test file. You can create a test file using a vi editor Find and replace with sed s/regexp/replacement/ Attempt to match regexp against the pattern space. If success- ful, replace that portion matched with replacement. $ cat -n sedexample 1 This is a test file. 2 You can create a test file using a vi editor 3 vi test file 4 vi is a powerfull editor. $ sed 's/test/Test/' sedexample This is a Test file. You can create a Test file using a vi editor vi Test file vi is a powerfull editor. First occurrence of the search string has been replaced, if there are more that one string to be replaced.Then we have to use the the example below. Use the g command to indicate to sed that it should examine the entire line instead of stopping at the first occurrence of your string: $ sed 's/test/TesT/g' sedexample This is a TesT file. You can create a TesT file using a vi editor vi TesT file vi is a powerfull editor. To insert a string at the beginning of each line of a file, for instance for quoting: $ sed 's/^/>> /' sedexample >> This is a test file. >> You can create a test file using a vi editor >> vi test file >> vi is a powerfull editor. Insert some string at the end of each line: $ sed 's/$/(end of line)/' sedexample This is a test file.(end of line) You can create a test file using a vi editor(end of line) vi test file(end of line) vi is a powerfull editor.(end of line) Multiple find and replace commands are separated with individual -e options: sed -e 's/test/TEST/g' -e 's/a/A/g' sedexample This is A TEST file. You cAn creAte A TEST file using A vi editor vi TEST file vi is A powerfull editor. By default sed prints its results to the standard output, most likely your terminal window. |