Hua
8 years ago
#unix #sed
latest #14
Hua
8 years ago
double space a file
sed G file
Hua
8 years ago
double space a file which already has some blank lines in it
sed '/^$/d;G'
Hua
8 years ago
triple space a file
sed 'G;G' file
立即下載
Hua
8 years ago
undo double-spacing (assumes all even-numbered lines are always blank)
sed 'n;d' file
Hua
8 years ago
insert a blank line above every line which matches "regex"
sed '/regex/{x;p;x}'
Hua
8 years ago
insert a blank line below every line which matches "regex"
sed '/regex/G'
Hua
8 years ago
insert a blank line above and below every line which matches "regex"
sed '/regex/{x;p;x;G}'
Hua
8 years ago
number each line of a file (like grep -n "^")
sed = file | sed 'N;s/\n/:/'
Hua
8 years ago
number each line of a file (like cat -n)
sed = file | sed 'N;s/^/ /;s/\n/\t/'
Hua
8 years ago
number each line of file, but only print numbers if line is not blank
sed '/./=' file | sed '/./N;s/\n/ /'
Hua
8 years ago
count lines (like "wc -l")
sed -n '$='
Hua
8 years ago
delete leading whitespace (spaces, tabs) from front of each line
sed 's/^[ \t]*//'
Hua
8 years ago
delete trailing whitespace (spaces, tabs) from end of each line
sed 's/[ \t]*$//'
Hua
8 years ago
delete BOTH leading & trailing whitespace from each line
sed 's/^[ \t]//;s/[ \t]$//'
back to top