Push your sed skills to the limit with these challenging exercises
These exercises are designed to be challenging. Don't hesitate to consult the sed manual (man sed) or experiment with different approaches. Remember, there's often more than one way to solve a problem with sed!
Write a sed script that reverses the order of words in each line of a file, while maintaining the original order of the lines.
Explanation: This solution uses multiple sed commands. It first separates words with newlines, then reverses the order of lines, and finally joins the words back together.
Write a sed command to convert camelCase variables to snake_case.
Explanation: This command looks for lowercase letters or numbers followed by uppercase letters, and inserts an underscore between them while converting the uppercase letter to lowercase.
Write a sed script to remove HTML comments (<!-- ... -->) from an HTML file, including multi-line comments.
Explanation: This script uses a loop to handle multi-line comments. It removes comments that are on a single line and accumulates lines for multi-line comments until the end of the comment is found.
Create a simple encryption/decryption sed script that rotates each letter by 13 positions (ROT13).
Explanation: This uses sed's transliteration command (y) to replace each letter with its ROT13 equivalent. The same script can be used for both encryption and decryption.
Write a sed command to convert dates from MM/DD/YYYY format to YYYY-MM-DD format.
Explanation: This command uses capture groups to rearrange the parts of the date. Note the use of # as a delimiter instead of / to avoid escaping the forward slashes in the date.
Create a sed script that adds line numbers to non-empty lines in a file, while ignoring empty lines.
Explanation: This script deletes empty lines, inserts line numbers, pads them with spaces, and adds commas for thousands.
Write a sed script that reverses the order of every N lines in a file (where N is a parameter).
Explanation: This script uses the hold space to accumulate lines and prints them in reverse order every N lines. Modify the '3~3' part to change N.
Create sample input files to test these solutions. Try modifying the exercises or combining multiple techniques to solve more complex text processing tasks.