###################################################################### ## Here are some helpful hints for sed. ## Written ?????? -Dunno ## Updated 020307 -Ed Arizona ## ## Questions or problems? UnixAdmin@ArizonaEd.com ## Visit the unix archive at http://www.arizonaed.com/unix ## Check out the long list of links, http://www.arizonaed.com/unix/urls.html ## ## Please feel free to email me about possible job opportunities in ## the NY/NJ/PA area. ## ###################################################################### # 1. Double space a file sed G file # 2. Triple space a file sed 'G;G' file # 3. Under UNIX: convert DOS newlines (CR/LF) to Unix format sed 's/.$//' file # assumes that all lines end with CR/LF sed 's/^M$// file # in bash/tcsh, press Ctrl-V then Ctrl-M # 4. Under DOS: convert Unix newlines (LF) to DOS format sed 's/$//' file # method 1 sed -n p file # method 2 # 5. Delete leading whitespace (spaces/tabs) from front of each line # (this aligns all text flush left). '^t' represents a true tab # character. Under bash or tcsh, press Ctrl-V then Ctrl-I. sed 's/^[ ^t]*//' file # 6. Delete trailing whitespace (spaces/tabs) from end of each line sed 's/[ ^t]*$//' file # see note on '^t', above # 7. Delete BOTH leading and trailing whitespace from each line sed 's/^[ ^t]*//;s/[ ^]*$//' file # see note on '^t', above # 8. Substitute "foo" with "bar" on each line sed 's/foo/bar/' file # replaces only 1st instance in a line sed 's/foo/bar/4' file # replaces only 4th instance in a line sed 's/foo/bar/g' file # replaces ALL instances within a line # 9. Substitute "foo" with "bar" ONLY for lines which contain "baz" sed '/baz/s/foo/bar/g' file # 10. Delete all CONSECUTIVE blank lines from file except the first. # This method also deletes all blank lines from top and end of file. # (emulates "cat -s") sed '/./,/^$/!d' file # this allows 0 blanks at top, 1 at EOF sed '/^$/N;/\n$/D' file # this allows 1 blank at top, 0 at EOF # 11. Delete all leading blank lines at top of file (only). sed '/./,$!d' file # 12. Delete all trailing blank lines at end of file (only). sed -e :a -e '/^\n*$/N;/\n$/ba' file # 13. If a line ends with a backslash, join the next line to it. sed -e :a -e '/\\$/N; s/\\\n//; ta' file # 14. If a line begins with an equal sign, append it to the # previous line (and replace the "=" with a single space). sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D' file # 15. Specify a group for re-ordering of the data stream. # echo 22.32.356.43 | \ sed 's/\([^ )]*\)\.\([^ )]*\)\.\([^ )]*\)\.\([^ )]*\)/\3\.\4\.\2\.\1/' # replace only between lines 1 and 20 1,20 s/Johnson/White/g # replace everywhere EXCEPT between lines 1 and 20 1,20 !s/Johnson/White/g # replace only between words "foo" and "bar" /foo/,/bar/ { s/Johnson/White/g; s/Smith/Wesson/g; } # replace only from the words "ENDNOTES:" to the end of file /ENDNOTES:/,$ { s/Schaff/Herzog/g; s/Kraft/Ebbing/g; } How do I change only the first occurrence of a pattern? To replace the regex "LHS" with "RHS", do this: gsed '0,/LHS/s//RHS/' # GNU sed 3.02a sed -e '1s/LHS/RHS/;t' -e '1,/LHS/s//RHS/' # other seds