The following is a collection of sed. They are from the following blogs.
1 2 3 4 5 6 |
|
I will pick some I that I think might be useful for me and listed them here.
1.Insert a blank line above every line that matches “regex”.
1
|
|
2.Insert a blank line below every line that matches “regex”.
1
|
|
3.Insert a blank line above and below every line that matches “regex”.
1
|
|
4.Number each line of a file (named filename). Left align the number.
1
|
|
5.Convert DOS/Windows newlines (CRLF) to Unix newlines (LF). (All 3 have not been tested!)
1 2 3 |
|
6.Convert Unix newlines (LF) to DOS/Windows newlines (CRLF). (All 3 have not been tested!)
1 2 3 |
|
7.Delete leading whitespace (tabs and spaces) from each line.
1
|
|
8.Delete trailing whitespace (tabs and spaces) from each line.
1
|
|
9.Delete both leading and trailing whitespace from each line.
1
|
|
10.Insert five blank spaces at the beginning of each line.
1
|
|
11.Substitute (find and replace) the fourth occurrence of “foo” with “bar” on each line.
1
|
|
12.Substitute (find and replace) the first occurrence of a repeated occurrence of “foo” with “bar”.
1
|
|
13.Substitute all occurrences of “foo” with “bar” on all lines that contain “baz”.
1
|
|
14.Substitute all occurrences of “foo” with “bar” on all lines that DO NOT contain “baz”.
1
|
|
15.Change text “scarlet”, “ruby” or “puce” to “red”.
1
|
|
16.Add a blank line after every five lines.
1
|
|
17.Print the first 10 lines of a file (emulates “head -10”).
1
|
|
18.Print only the lines that match a regular expression (emulates “grep”).
1
|
|
19.Print only the lines that do not match a regular expression (emulates “grep -v”).
1
|
|
20.Print the line immediately before regexp, but not the line containing the regexp.
1
|
|
21.Print the line immediately after regexp, but not the line containing the regexp.
1
|
|
22.Print one line before and after regexp. Also print the line matching regexp and its line number. (emulates “grep -A1 -B1”).
1
|
|
23.Grep for “AAA” and “BBB” and “CCC” in any order.
1
|
|
24.Grep for “AAA” and “BBB” and “CCC” in that order.
1
|
|
25.Grep for “AAA” or “BBB”, or “CCC”.
1
|
|
26.Print only the lines that are 65 characters in length or more.
1
|
|
27.Print only the lines that are less than 65 chars.
1
|
|
28.Print section of a file from a regex to end of file.
1
|
|
29.Print lines 8-12 (inclusive) of a file.
1
|
|
30.Print line number 52.
1
|
|
31.Beginning at line 3, print every 7th line.
1
|
|
32.Print section of lines between two regular expressions (inclusive).
1
|
|
33.Print all lines in the file except a section between two regular expressions.
1
|
|
34.Delete duplicate, consecutive lines from a file (emulates “uniq”).
1
|
|
35.Delete all lines except duplicate consecutive lines (emulates “uniq -d”).
1
|
|
36.Delete the first 10 lines of a file.
1
|
|
37.Delete the last line of a file.
1
|
|
38.Delete the last 2 lines of a file.
1
|
|
39.Delete lines that match regular expression pattern.
1
|
|
40.Delete all blank lines in a file (emulates “grep ‘.’”.
1
|
|
41.Delete all consecutive blank lines from a file (emulates “cat -s”).
1
|
|
42.Delete all leading blank lines at the top of a file.
1
|
|
43.Delete all trailing blank lines at the end of a file.
1
|
|