Advanced sed Exercises

Push your sed skills to the limit with these challenging exercises

Tip:

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!

Exercise 1: Reverse Words in Each Line

Write a sed script that reverses the order of words in each line of a file, while maintaining the original order of the lines.

Solution:

sed 's/\([^[:space:]]\+\)[[:space:]]*/&\n/g' input.txt | sed '1!G;h;$p' | sed 'N;s/\n//g'

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.

Exercise 2: Convert Camel Case to Snake Case

Write a sed command to convert camelCase variables to snake_case.

Solution:

sed -E 's/([a-z0-9])([A-Z])/\1_\L\2/g'

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.

Exercise 3: Remove HTML Comments

Write a sed script to remove HTML comments (<!-- ... -->) from an HTML file, including multi-line comments.

Solution:

sed -e :a -e 's/<!--.*-->//g;/<!--/N;//ba'

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.

Exercise 4: Encrypt/Decrypt Text

Create a simple encryption/decryption sed script that rotates each letter by 13 positions (ROT13).

Solution:

sed 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM/'

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.

Exercise 5: Reformat Dates

Write a sed command to convert dates from MM/DD/YYYY format to YYYY-MM-DD format.

Solution:

sed -E 's#([0-9]{2})/([0-9]{2})/([0-9]{4})#\3-\1-\2#g'

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.

Exercise 6: Add Line Numbers to Non-Empty Lines

Create a sed script that adds line numbers to non-empty lines in a file, while ignoring empty lines.

Solution:

sed '/^$/d;=;s/^/ /;s/^ *\([0-9]\+\)\([0-9]\{3\}\)/\1,\2/'

Explanation: This script deletes empty lines, inserts line numbers, pads them with spaces, and adds commas for thousands.

Exercise 7: Reverse Every N Lines

Write a sed script that reverses the order of every N lines in a file (where N is a parameter).

Solution:

sed -n '1!G;h;3~3p' # Example for N=3

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.

Practice Tip:

Create sample input files to test these solutions. Try modifying the exercises or combining multiple techniques to solve more complex text processing tasks.






Scroll to Top