The 'sed' command, short for stream editor, is a versatile and powerful utility in Linux for text manipulation. It operates on text files line by line, allowing you to perform a wide range of editing operations. In this comprehensive guide, we will delve into the 'sed' command and explore practical examples that showcase its capabilities. By mastering 'sed', you'll gain the skills to effortlessly manipulate text on the Linux platform. Let's dive in!
1. Replace Text:
9. Print Lines Matching a Pattern:
sed -n '/pattern/p' filename.txt
sed '/pattern/a\New line to append' filename.txt
sed '/pattern/i\New line to insert' filename.txt
sed 's/$/ Appended Text/' filename.txt
sed '/^$/d' filename.txt
sed 's|old-text|new-text|' filename.txt
sed 's/\<[A-Z]\+>/\L&/g' filename.txt
sed 's/\<[a-z]\+>/\U&/g' filename.txt
sed -n '/pattern/!p' filename.txt
sed '=' filename.txt | sed 'N;s/\n/ /'
sed 's/^[ \t]*//' filename.txt
sed '1!G;h;$!d' filename.txt
Conclusion:
By mastering the 'sed' command, you unlock a world of possibilities for manipulating text in Linux. Whether it's replacing text, deleting lines, printing specific ranges, or performing pattern-based operations, 'sed' offers the versatility you need. Incorporate these examples into your workflow, and you'll become proficient in effortlessly manipulating text files on the Linux platform. Empower yourself with the power of 'sed' and take your text manipulation skills to new heights.