sed 's/original text/replacement text/g' file.txt
sed -n '1~2p' file.txt
sed -i '2d' file.txt
sed -n '/pattern/p' file.txt
sed -n '/pattern/!p' file.txt
sed -n 'n,$p' file.txt
sed -i 'n i new line' file.txt
sed -i 'n a new line' file.txt
sed -i '/pattern/d' file.txt
sed -i 's/original text/new text/g' file.txt
When the sed command is used with the -i option, sed will create a backup file of the original file being edited with the extension you provide. Then, sed will write the modified content to the original file, effectively modifying the file in place.
For example, suppose there is a file named example.txt, and you want to use sed to edit the file and create a backup file during the modification process. You can use the following command:
sed -i.bak 's/foo/bar/g' example.txt
In this command, -i.bak indicates that a backup file named example.txt.bak will be created during the editing process, and 's/foo/bar/g' indicates that all occurrences of "foo" within the file should be replaced with "bar". This operation will modify the example.txt file directly, and backup the original content to example.txt.bak.
Note that while the -i option can be a convenient way to perform editing operations, it should be used with caution, especially when working with important files. Careless use of the -i option can result in data loss or irreversible changes. When using the -i option, it is recommended to backup important files beforehand so that you can revert to the previous state if necessary.