![]() |
Software > Linux tools >
(on Wikipedia)
https://www.gnu.org/software/sed/ [ 1 ]
Generally used to do search/replace on strings, but also in text files.
- tutorial: do it with sed
-
Useful one-line scripts for sed:
Table of Contents [hide]
Delete the end off of filenames ∞
The equivalent of some sort of DOS-style mv *sed- \1
ls -d *sed- | sed 's/\(.*\).sed-$/mv "&" "\1"/' | sh
dos2unix ∞
Instead of dos2unix, do:
sed 's/^M$//' < dos.txt > unix.text
sed 's/.$//' < dos.txt > unix.text
unix2dos ∞
Instead of unix2dos, do:
sed 's/$/^M/' < unix.text > dos.txt
Footer removal ∞
A sed script to remove the footer from an email.
#!/bin/sh footstart="_______________________________________________________________" footend="REMOVE THIS FOOTER FROM YOUR QUOTES" sed -e "/$footstart/,/$footend/d"
My take on this:
Clean up this command:
rsync --dry-run -vrlt /source/dir /dest/dir > filename.txt
Spoiler
sending incremental file list ./ index.htm index.txt test.html sent 89 bytes received 24 bytes 226.00 bytes/sec total size is 164 speedup is 1.45 (DRY RUN)
footstart="sending incremental file list" footend="\.\/" sed -e "/$footstart/,/$footend/d" < filename.txt > filename2.txt footstart="^$" footend="^total size is * speedup is * (DRY RUN)" sed -e "/$footstart/,/$footend/d" < filename2.txt
Regular expressions ∞
See Regular expressions.
Something I used for YouTube RSS feeds:
sed 's*.\**<outline title="Videos uploaded by \0" text="Videos uploaded by \0" type="rss" version="RSS" xmlUrl="http://www.youtube.com/rss/user/\0/videos.rss" htmlUrl="http://www.youtube.com/profile_videos?user=\0"/>*' ~/subs-nofaves.txt > ~/subs-nofaves2.txt
Search/replace a string in an echo/variable ∞
\echo 'source string' | \sed --expression='s/source/target/'
Search/replace a string in a file ∞
This modifies the file in-place (essentially overwriting it). Make a backup first.
Basic ∞
\sed --in-place --expression='s/source/target/g' filename.txt # This makes a backup automatically \sed --in-place='.bak' --expression='s/source/target/g' filename.txt
Search/replace strings in many file, using slashes ∞
Replace the use of / with |
for i in * ; do \sed --in-place --expression='s|/path/to|/new/path/to|g' "$i" ; done
Last updated 2022-10-16 at 04:37:34


added search/replace with a variable