In this tutorial, we’ll show, with examples that you can copy paste right into your shell, how you can use sed to automate mind-numbing tasks. Automating mind-numbing tasks is not only good for your emotional health, it’ll make you more accurate and productive
macOS (BSD) disclaimer
If you’re using a mac, the -i flag requires a file extension (like .bak) which creates backup files that then have to be removed. If you’re using linux, everything will work as is.
The Examples
Backup a list of files
Example usage
ls | sed "s/.*/cp & &.bak/" | bash
Reusable script
#!/usr/bin/env bash
sed "s/.*/cp & &.bak/"
Explanation
This example is interesting because it uses sed to generate bash code which is executed by bash.
Imagine the input is a single line containing the text "file.txt"
Then sed finds .* which means the entire input value "file.txt".
It then replaces it with "cp & &.bak".
& is a special value which means the entire found match, which is file.
So the answer is: "cp file.txt file.txt.bak"
If the in…