Skip to content

sed

sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.


Print line 1 to line 200

sed -n '1,200p' /etc/profile

Print everything after the last dot

echo "org.bluez" | sed 's/.*\.//'

Comment out line 17 and add a # at column 1

sed '17s/^/#/ filename.txt

Add a character to begin of a line

echo "Add a asterix at the begin of this line"|sed 's/$/*/g'

Add a character to end of line

echo "Add a asterix at the end of this line"|sed 's/$/*/g'

Insert line above in the same line

echo hello | sed -e '/hello/i line above' -e '/hello/a line below'

Find all lines that begins on Example and delete one line below

sed -e '/^Example:/ { N; d; }' file.txt

Generate fake mac addresses from bluetoothctl devices to hide our real mac addresses online

bluetoothctl devices |
sed 's/^Device //' |
awk '
BEGIN { srand() }
{
    printf "Device %02X:%02X:%02X:%02X:%02X:%02X %s\n",
        rand()*256, rand()*256, rand()*256,
        rand()*256, rand()*256, rand()*256,
        substr($0, index($0,$2))
}'