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.
Make every character after a doublequote uppercase
sed -i -E 's/"([a-z])/"\U\1/g' filename.txt
Make every character after a doublequote uppercase
sed -i -E 's/"([A-Z])/"\L\1/g' filename.txt
Add text above PATTERN
cat filename.txt| sed '/PATTERN/i Text Above PATTERN' file
Add # Added by script above all lines that contains Listen
cat filename.txt | sed '/Listen/i # Added by script'
Add # This text will be added below PATTERN
history
sed '/PATTERN/a # This text will be added below' file
This will add markdown syntax for bash around netstat
cat netstat.md |sed '/netstat/i !!! Example ""\n\n```bash'|sed '/netstat/a ```'
This command will add !!! Example and backdicks around netstat
cat netstat.md |sed '/netstat/i !!! Example ""\n\n```bash'|sed '/netstat/a ```'
Delete empty lines
sed -i '/^\s*$/d' # Deleting Lines Containing Just Whitespace Characters
sed '/^\ *$/d'
sed '/^$/d' # Change $ to any character
sed '/^$/d'
sed -r '/^\s*$/d'
sed '/^[[:space:]]*$/d'
sed '/^[[:space:]]*$/da'
sed -n '/[^[:space:]]/p'
sed '/^$/d'
sed -n '/^\s*$/!p'
sed -E '/^$/d'
sed -n '/ /p'
sed -r "/^\r?$/d"
sed -e '/^[ ]*$/d'
Print the first 8 characetrs from 0124 file
sed -e 's/^\(.\{8\}\).*/\1/' /proc/rip/0124
Delete all lines that begins with #
If you wanted to keep the first word of a line, and delete the rest of the line, mark the important part with the parenthesis
echo ABCDabcd123 | sed 's/\([A-Z]*\).*/\1/'
Merge lowercase from second string to string one and keep numbers on second string
echo abcd123 edfh456 | sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/'
Set parenthesis around the first word
echo abcd123 abcd123 edfh456 | sed 's/[^ ]*/(&)/'
Set parentesis around the first character
echo abcd123 abcd123 edfh456 | sed 's/[^ ]/(&)/'
Set parenthesis around the three first letters
echo abcd123 abcd123 edfh456 | sed 's/[^ ][^ ][^ ]/(&)/'
Set parenthesis around the entire line
echo abcd123 abcd123 edfh456 | sed 's/[^ ].*/(&)/'
echo abcd123 abcd123 edfh456 | sed 's/[^ ].*/(&)/g'
Set parenthesis around each word (/g = Global Replacement)
echo abcd123 abcd123 edfh456 | sed 's/[^ ][^ ]*/(&)/g'
Reverse the first three characters on a line
echo abcd123 abcd123 edfh456 | sed 's/^\(.\)\(.\)\(.\)/\3\2\1/'
Keep the first word of a line that contains lowercase characters and delete everything after the first word
echo abcd123 efgh123 ijkl456 | sed 's/\([a-z]*\).*/\1/'
Take the first numbers from first string and write it after the first word
echo abcd123 efgh123 ijkl456 | sed -r 's/[0-9]+/& &/'
Eliminate duplicated words (merge word 1 and word 2
echo abcd123 efgh123 ijkl456 | sed 's/\([a-z]*\) \1/\1/'
Merge string 1 and string 2
echo abcd123 efgh123 ijkl456 | sed 's/[a-zA-Z]* //1'
Merge string 2 and string 3
echo abcd123 efgh123 ijkl456 | sed 's/[a-zA-Z]* //2'
Leave the first word alone but change the second
echo abcd123 efgh123 ijkl456 | sed 's/[a-zA-Z0-9]* /<- ReplacedSecondWord -> /2g'
Delete the first word from the string
echo abcd123 efgh123 ijkl456 | sed 's/[^ ]*//1'
Delete the first word from the string
echo abcd123 efgh123 ijkl456 | sed 's/[^ ']*//1
Delete second word from the string
echo abcd123 efgh123 ijkl456 | sed 's/[^ ]*//2'
Delete third word from the string
echo abcd123 efgh123 ijkl456 | sed 's/[^ ]*//3'
Delete the sixth field from /etc/passwd
sed 's/[^:]*//6' < /etc/passwd
Delete the seventh field from /etc/passwd
sed 's/[^:]*//7' < /etc/passwd
Delete the third field in /etc/passwd"
sed 's/^\([^:]*\):[^:]:/\1::/' </etc/passwd
Insert a word on third field in /etc/passwd
sed 's/^\([^:]*\):[^ ]:/\1:ADDED_NEW_WORD_HERE:/' </etc/passwd
Delete the first field in /etc/passwd
sed 's/[^:]*:/:/1' </etc/passwd
Delete the second field in /etc/passwd
sed 's/[^:]*:/:/2' </etc/passwd
Add :INSERT_ME_HERE: after the 2th character in /etc/passwd
sed 's/./&:INSERT_ME_HERE:/2' < /etc/passwd
Add :INSERT_ME_HERE: after 2th character in /etc/passwd (dots are used instead of number for set field number)
sed 's/^.../&:INSERT_ME_HERE:/' < /etc/passwd
Print only if we matching our pattern so in this example we match dnsmasq and if it exist in /etc/passwd we print the dnsmasq line
sed -n 's/dnsmasq/&/p' < /etc/passwd
This flag makes the pattern match case sensitive. This will match abc, aBc, ABC, AbC
sed -n '/DnSmAsQ/I p' < /etc/passwd
Replace the matched word wich is the first s/word1/replace_word1_with_this_line/
sed 's/nginx/replace_word1_with_this_line/' < /etc/passwd
Replace two words but we should never use two processes wich this is when we can do one
sed 's/nginx/replace_word1_with_this_line/' < /etc/passwd | sed 's/nologin/replace_account/'
Combining multiple commands is to use -e before each command
sed -e 's/nginx/replace_word1_with_this_line/' -e 's/nologin/replace_nologin/' < /etc/passwd
The long argument version for combining multiple commands is to use below instead of above
sed --expression='s/nginx/replace_word1_with_this_line/' --expression='s/nologin/replace_nologin/' < /etc/passwd
Remove all lines that begins with an uppercase character and grep all comment hashes
sddm --example-config|sed 's/^[A-Z].*//' |grep '#'
If nginx is in the file, then each line that has this is printed twice above/below each other
sed 's/nginx/&/p' /etc/passwd
sed '/nginx/p' /etc/passwd
Adding -n option and the example below acts like grep
sed -n 's/nginx/&/p' /etc/passwd
The long argument of n is --quiet or --silent (all three commands below is equal)
sed -n 's/nginx/&/p' /etc/passwd
sed --silent 's/nginx/&/p' /etc/passwd
sed --quiet 's/nginx/&/p' /etc/passwd
Print matched pattern twice if we match a character otherwise we don´t print the match twice (/p is required otherwise we get an error`)
Delete line 180 /etc/config/filename.conf
sed -i '180d' /etc/config/filename.conf
Remove all = that is included between rows 60 and 124
sed -i '60,124s/= //g' filename.md
Remove lowercase characters from second string
echo abcd123 edfh456 | sed 's/\([a-z]*\) \([a-z]*\)/\1 \1/'
Delete two patterns - All lines that begins on [A-Z] or a #
sddm --example-config|sed '/^#/,/^[A-Z]/d'
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))
}'
Add same value inside [] and ()
ls -1 | sed 's/^\(.*\)$/[\1](\1)/'
[1](2 - Replace 1 with 2 and vice versa
sed -E 's/!\[([^]]+)\]\(([^)]+)\)//g' index2.md > index2_fixed.md