sed tricks
Select
Specified lines
cat -n big_file.txt | sed -n 100,150p
Insert
Insert line at the beginning of the file
sed -i '1 i[New string value]' /path/to/file
to any line by number
sed -i '[NUM] i[New string value]' /path/to/file
Replace
Simple replace one value by other
sed -i 's/OldValue/NewValue/g' /path/to/file
Replace /home10/incorrect_path
by /home10/correct_path
at line that start from Value1 in file /etc/proftpd/user2
sed -ei '/^Value1/ s|/home10/incorrect_path|/home10/correct_path|' /etc/proftpd/user2
Split long line by word count
cat 1.txt
1 2 3 4 5.3 6,8 7 8 10
a b c d e hh jj kk sds-ds kkoo
cat 1.txt | sed 's/ /\n/3;P;D'
1 2 3
4 5.3 6,8
7 8 10
a b c
d e hh
jj kk sds-ds
kkoo
Highlighting
cat 1.txt
aaa bbb ccc
cat 1.txt| sed -e 's/\(bbb\)/\x1B[32m\1\x1B[39m/'
Delete
Delete line by pattern
sed -i '/Pattern/d' /path/to/file
Delete numbers
sed -i 's/[0-9]*//g' /path/to/file
Delete domains from email
cat 1.txt | sed 's/@[^ ]*//'
Delete ESC sequences from file
sed -i "s/\x1B\[[0-9;]*[a-zA-Z]//g" /path/to/file
Remove commented by # and empty lines
sed -i 's/#.*$//;/^$/d' /path/to/file
Delete line by number
sed -i [LINE_NUMBER]d /path/to/file
Delete lines from {NUMLINE} to end of file
sed -i '{NUMLINE},$ d' /path/to/file
Delete last line in file
# Linux
sed -i '$d' /path/to/file
# FreeBSD, MacOS
sed -i '' -e '$ d' /path/to/file
Delete pattern in file without changing inode of file
sed '/PATTERN/d' < test.log >/tmp/123.tmp
cat /tmp/123.tmp > test.log
rm -f /tmp/123.tmp
Delete file name from full path
echo "/opt/data/nonprod/variables.tf" | sed 's|/[^/]*$||'
# /opt/data/nonprod