Sed
- Replace a string in a file with a newstring
- Find and delete a line
- Remove empty lines
- Remove the first or n lines in a file
- To replace tabs with spaces
- Show defined lines from a file
- Substitute within Vim editor
- And many more
Examples¶
Tip
The flag -i makes the changes permanent while running the sed command
Replace the oldWord with the newWord globally (Only on screen)¶
sed 's/oldWord/newWord/g' fileName
Replace the oldWord with the newWord globally (make changes permanent)¶
sed -i 's/oldWord/newWord/g' fileName
Find and delete the keyWord¶
sed 'keyWord/d' fileName
Delete an empty line from a file
sed '/^$/d' fileName
Delete first line
sed '1d' fileName
Delete first two lines
sed '1,2d' fileName
Replace Tabs with Spaces
sed 's/\t/ /g' fileName
View from line i to j
sed -n i,jp fileName
sed -n 12,18p fileName #view from line 12 to 18
View all except from i to j
sed i,jd fileName
Add space after each line
sed G fileName
Replace oldWord with newWord except the ith occurence
sed 'i!s/oldWord/newWord/g' fileName
Search and replace in the Vim editor
:s/oldWord/newWord