sed
sed stands for stream editor.
Tips
Warning
Please be aware that the popular -i (in-place editing) flag
of sed is not portable. It also
creates a new file in the background and overrides the old one
which will break symlinks.
dry run
By default, sed writes the whole pattern space to stdout, even if no
edits have occured. To "dry run" sed, silence the automatic print with
-n and print the line with edits like this:
sed -n 's/Hello/Bye/gp' <some_file>
replace pattern inplace
In one file
sed -i 's/REGEX_PATTERN_TO_SEARCH_FOR/REPLACEMENT/g' INPUTFILE
or in conjunction with find for multiple files
find . -type f -exec sed -i 's/foo/bar/g' {} +
Be aware of the in-place editing pitfalls.
append to file
... in the beginning
You can use
sed -i "1s/^/TEXT TO PREPEND/" <FILE>
for a single file. In conjunction with find you can run sed on
multiple files like so:
find *.md -type f -exec sed -i "1s/^/TEXT TO PREPEND/" {} +
Be aware of the in-place editing pitfalls.
... at the end
No need to use sed. Use the >> operator like so for one file
echo "Good bye!" >> hello_world.txt
or the tee utility for multiple files:
echo "Good bye!" | tee -a *.md
print nth line of output
To only show a selection of lines from a previous command, use sed like
so:
command | sed -n '2p' # prints second line
command | sed -n '5,10p' # prints lines 5 to 10