Steven's Blog

A Dream Land of Peace!

Sed一行命令(one-liners)

The following is a collection of sed. They are from the following blogs.

1
2
3
4
5
6
http://www.catonmat.net/blog/sed-one-liners-explained-part-one/
http://www.catonmat.net/blog/sed-one-liners-explained-part-two/
http://www.catonmat.net/blog/sed-one-liners-explained-part-three/

http://www.grymoire.com/Unix/Sed.html
http://www.catonmat.net/blog/wp-content/uploads/2008/09/sed1line.txt

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
cat file | sed '/regex/{x;p;x;}'

2.Insert a blank line below every line that matches “regex”.

1
cat file | sed '/regex/G'

3.Insert a blank line above and below every line that matches “regex”.

1
cat file | sed '/regex/{x;p;x;G;}'

4.Number each line of a file (named filename). Left align the number.

1
sed = filename | sed 'N;s/\n/\t/'

5.Convert DOS/Windows newlines (CRLF) to Unix newlines (LF). (All 3 have not been tested!)

1
2
3
cat file | sed 's/.$//'
cat file | sed 's/^M$//'
cat file | sed 's/\x0D$//'

6.Convert Unix newlines (LF) to DOS/Windows newlines (CRLF). (All 3 have not been tested!)

1
2
3
sed 's/$/\r/'
sed "s/$//"
sed "s/$/`echo -e \\\r`/"

7.Delete leading whitespace (tabs and spaces) from each line.

1
cat file | sed 's/^[ \t]*//'

8.Delete trailing whitespace (tabs and spaces) from each line.

1
cat file | sed 's/[ \t]*$//'

9.Delete both leading and trailing whitespace from each line.

1
cat file | sed 's/^[ \t]*//;s/[ \t]*$//'

10.Insert five blank spaces at the beginning of each line.

1
cat file | sed 's/^/     /'

11.Substitute (find and replace) the fourth occurrence of “foo” with “bar” on each line.

1
cat file | sed 's/foo/bar/4'

12.Substitute (find and replace) the first occurrence of a repeated occurrence of “foo” with “bar”.

1
cat file | sed 's/\(.*\)foo\(.*foo\)/\1bar\2/'

13.Substitute all occurrences of “foo” with “bar” on all lines that contain “baz”.

1
cat file | sed '/baz/s/foo/bar/g'

14.Substitute all occurrences of “foo” with “bar” on all lines that DO NOT contain “baz”.

1
cat file | sed '/baz/!s/foo/bar/g'

15.Change text “scarlet”, “ruby” or “puce” to “red”.

1
cat file | sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g'

16.Add a blank line after every five lines.

1
cat file | sed 'n;n;n;n;G;'

17.Print the first 10 lines of a file (emulates “head -10”).

1
cat file | sed 10q

18.Print only the lines that match a regular expression (emulates “grep”).

1
cat file | sed -n '/regexp/p'

19.Print only the lines that do not match a regular expression (emulates “grep -v”).

1
cat file | sed -n '/regexp/!p'

20.Print the line immediately before regexp, but not the line containing the regexp.

1
cat file | sed -n '/regexp/{g;1!p;};h'

21.Print the line immediately after regexp, but not the line containing the regexp.

1
cat file | sed -n '/regexp/{n;p;}'

22.Print one line before and after regexp. Also print the line matching regexp and its line number. (emulates “grep -A1 -B1”).

1
cat file | sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

23.Grep for “AAA” and “BBB” and “CCC” in any order.

1
cat file | sed '/AAA/!d; /BBB/!d; /CCC/!d'

24.Grep for “AAA” and “BBB” and “CCC” in that order.

1
cat file | sed '/AAA.*BBB.*CCC/!d'

25.Grep for “AAA” or “BBB”, or “CCC”.

1
cat file | sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d

26.Print only the lines that are 65 characters in length or more.

1
cat file | sed -n '/^.\{65\}/p'

27.Print only the lines that are less than 65 chars.

1
cat file | sed '/^.\{65\}/d'

28.Print section of a file from a regex to end of file.

1
cat file | sed -n '/regexp/,$p'

29.Print lines 8-12 (inclusive) of a file.

1
cat file | sed -n '8,12p'

30.Print line number 52.

1
cat file | sed -n '52p'

31.Beginning at line 3, print every 7th line.

1
cat file | sed -n '3,${p;n;n;n;n;n;n;}'

32.Print section of lines between two regular expressions (inclusive).

1
cat file | sed -n '/Iowa/,/Montana/p'

33.Print all lines in the file except a section between two regular expressions.

1
cat file | sed '/Iowa/,/Montana/d'

34.Delete duplicate, consecutive lines from a file (emulates “uniq”).

1
cat file | sed '$!N; /^\(.*\)\n\1$/!P; D'

35.Delete all lines except duplicate consecutive lines (emulates “uniq -d”).

1
cat file | sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'

36.Delete the first 10 lines of a file.

1
cat file | sed '1,10d'

37.Delete the last line of a file.

1
cat file | sed '$d'

38.Delete the last 2 lines of a file.

1
cat file | sed 'N;$!P;$!D;$d'

39.Delete lines that match regular expression pattern.

1
cat file | sed '/pattern/d'

40.Delete all blank lines in a file (emulates “grep ‘.’”.

1
cat file | sed '/^$/d'

41.Delete all consecutive blank lines from a file (emulates “cat -s”).

1
cat file | sed '/./,/^$/!d'

42.Delete all leading blank lines at the top of a file.

1
cat file | sed '/./,$!d'

43.Delete all trailing blank lines at the end of a file.

1
cat file | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'