Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comprehensive List of Sed Commands with Descriptions
#1
Comprehensive List of Sed Commands with Descriptions

**sed** (Stream Editor) is a powerful command-line utility used for parsing and transforming text in files or streams. It is commonly used for text substitution, deletion, and other forms of text processing. Below is a detailed list of sed commands, along with descriptions and examples.



1. Basic Text Substitution
Description: Replaces the first occurrence of a pattern in each line of a file or stream.
Code:
sed 's/old_text/new_text/' [file]
Example: To replace "foo" with "bar" in `example.txt`:
Code:
sed 's/foo/bar/' example.txt

2. Global Text Substitution
Description: Replaces all occurrences of a pattern in each line of a file or stream.
Code:
sed 's/old_text/new_text/g' [file]
Example: To replace all occurrences of "foo" with "bar" in `example.txt`:
Code:
sed 's/foo/bar/g' example.txt

3. Substitution with In-Place Editing
Description: Replaces text in a file and saves the changes directly to the file.
Code:
sed -i 's/old_text/new_text/g' [file]
Example: To replace all occurrences of "foo" with "bar" in `example.txt` and save the changes:
Code:
sed -i 's/foo/bar/g' example.txt

4. Case-Insensitive Substitution
Description: Replaces text in a case-insensitive manner.
Code:
sed 's/old_text/new_text/I' [file]
Example: To replace "Foo", "FOO", and "foo" with "bar":
Code:
sed 's/foo/bar/I' example.txt

5. Deleting Lines Matching a Pattern
Description: Deletes lines that match a specific pattern.
Code:
sed '/pattern/d' [file]
Example: To delete all lines containing the word "delete" in `example.txt`:
Code:
sed '/delete/d' example.txt

6. Deleting a Specific Line
Description: Deletes a specific line by line number.
Code:
sed 'Nd' [file]
Example: To delete the 3rd line in `example.txt`:
Code:
sed '3d' example.txt

7. Inserting Text Before a Line
Description: Inserts text before a specific line in a file.
Code:
sed 'N i\new_text' [file]
Example: To insert "Hello, World!" before the 2nd line in `example.txt`:
Code:
sed '2i\Hello, World!' example.txt

8. Appending Text After a Line
Description: Appends text after a specific line in a file.
Code:
sed 'N a\new_text' [file]
Example: To append "Goodbye!" after the 3rd line in `example.txt`:
Code:
sed '3a\Goodbye!' example.txt

9. Replacing Text on a Specific Line
Description: Replaces text only on a specific line.
Code:
sed 'Ns/old_text/new_text/' [file]
Example: To replace "foo" with "bar" only on the 4th line:
Code:
sed '4s/foo/bar/' example.txt

10. Replacing Text Between Two Lines
Description: Replaces text between two line numbers or patterns.
Code:
sed 'N,M s/old_text/new_text/g' [file]
Example: To replace "foo" with "bar" between lines 3 and 5:
Code:
sed '3,5 s/foo/bar/g' example.txt

11. Printing Only Matching Lines
Description: Prints only the lines that match a specific pattern.
Code:
sed -n '/pattern/p' [file]
Example: To print only lines containing "foo":
Code:
sed -n '/foo/p' example.txt

12. Printing Line Numbers
Description: Prints the line numbers along with the content of each line.
Code:
sed '=' [file] | sed 'N;s/\n/\t/'
Example: To print the line numbers along with the lines in `example.txt`:
Code:
sed '=' example.txt | sed 'N;s/\n/\t/'

13. Using Multiple Sed Commands
Description: Executes multiple sed commands in a single invocation.
Code:
sed -e 'command1' -e 'command2' [file]
Example: To replace "foo" with "bar" and delete lines containing "delete":
Code:
sed -e 's/foo/bar/g' -e '/delete/d' example.txt

14. Using Sed with Regular Expressions
Description: Uses regular expressions for more complex pattern matching.
Code:
sed 's/regex_pattern/replacement/' [file]
Example: To replace any sequence of digits with "number":
Code:
sed 's/[0-9]\+/number/g' example.txt

15. Displaying Help Information
Description: Displays help information for the sed command, listing all available options and their descriptions.
Code:
sed --help
Example: To display help information for sed:
Code:
sed --help



Conclusion

The **sed** command is a powerful and versatile tool for text processing, making it an essential utility for anyone working with text files or streams on Unix/Linux systems. By mastering these commands, you can efficiently manipulate text data, automate editing tasks, and streamline your workflow.

Happy Editing!
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)