Linux SED Commands and Usage
sed is a powerful and versatile text editor in the Unix/Linux environment. It is often used to perform batch transformations on text data and can be used to perform various operations such as find and replace, delete, insert and copy. Here are some commonly used sed commands and their usage for beginners:
1. Find and Replace: The following command can be used to replace all occurrences of a pattern with a different string in a file:
$ sed 's/old-pattern/new-pattern/g' input-file > output-file
2. Delete a Line: The following command can be used to delete a specific line from a file:
$ sed '5d' input-file > output-file
3. Insert a Line: The following command can be used to insert a line after a specific line in a file:
$ sed '5a new-line' input-file > output-file
4. Copy a Line: The following command can be used to copy a specific line to another location in the file:
$ sed '5c\new-line' input-file > output-file
5. Address Ranges: The following command can be used to perform operations on a range of lines in a file:
$ sed '2,4d' input-file > output-file
6. In-Place Editing: The following command can be used to perform in-place editing of a file:
$ sed -i 's/old-pattern/new-pattern/g' input-file
7. Regular Expressions: sed supports the use of regular expressions to match patterns in text data. The following command can be used to replace a pattern that matches a regular expression:
$ sed 's/[0-9]*/new-pattern/g' input-file > output-file
Post a Comment
Post a Comment