Comprehensive List of Sed Commands with Descriptions - Printable Version +- WildlandsTech (https://wildlandstech.com) +-- Forum: Programming (https://wildlandstech.com/forumdisplay.php?fid=3) +--- Forum: Batch & Shell Scripting (https://wildlandstech.com/forumdisplay.php?fid=42) +--- Thread: Comprehensive List of Sed Commands with Descriptions (/showthread.php?tid=159) |
Comprehensive List of Sed Commands with Descriptions - Sneakyone - 09-03-2024 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] 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] 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] 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] 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] Code: sed '/delete/d' example.txt 6. Deleting a Specific Line Description: Deletes a specific line by line number. Code: sed 'Nd' [file] 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] 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] 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] 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] 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] 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/' 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] 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] 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 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! |