3. TECHNICAL
3.1. More
detailed explanation of basic sed
3.1.3. Substitution switches
N - Nth match, if omitted, first match.
g - Global aka: ALL.ex: %s/xdx/xx/g
p - Print.
w file - Write pattern space to file if a replacement was done.
3.2. Common useful one-line sed scripts
———» | |
3.3. Addressing and address ranges
3.4. Address ranges in GNU sed and HHsed
3.5. Debugging sed scripts
the "=" command, prints the source line number
p
\i
+++ script point #4 +++
sd sed debugger
3.6. Notes about s2p, the sed-to-perl translator
3.7. GNU/POSIX extensions to regular expressions
|
3.2. Common useful one-line sed scripts
# for UNIX: convert DOS newlines (CR/LF) to Unix format
sed 's/^M$//' file
# for DOS: convert Unix newlines (LF) to DOS format
sed 's/$//' file or sed -n p file
# Delete leading whitespace (spaces | tabs)
# (this aligns all text flush left). ^t represents a true tab character.
sed 's/^[ ^t]*//' file
# trailing
sed 's/[ ^t]*$//' file
# Delete leading and trailing whitespace
sed 's/^[ ^t]*//;s/[ ^]*$//' file
# Substitute "foo" with "bar" ONLY for lines which contain "baz"
sed '/baz/s/foo/bar/g' file
# Delete all CONSECUTIVE null lines from file except the first.
# also deletes all nulllines from top and end of file. (emulates "cat -s")
sed '/./,/^$/!d' file # allows 0 null line at top, 1 at EOF
sed '/^$/N;/\n$/D' file # allows 1 null line at top, 0 at EOF
# Delete all leading null lines
sed '/./,$!d' file
# Delete all trailing null lines
sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' file
# If a line ends with a backslash, join the next line to it.
sed -e :a -e '/\\$/N; s/\\\n//; ta' file
# 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
4. EXAMPLES
|
ONE-CHARACTER QUESTIONS
4.01. insert a newline into
the RHS* of a substitution
4.02. representng control-codes or non-printable characters
4.03. convert files with toggle characters,
like +this+, to look like [i]this[/i]
CHANGING STRINGS
4.10. perform a case-insensitive search
4.11. match only the first occurrence of a pattern in a file.
4.12. parse a comma-delimited (CSV) data file
4.13. handle fixed-length, columnar data
4.14. commify a string of numbers (i.e.1234 -> 1,234)
GNU sed:
sed ':a ; s/\B[0-9]\{3\}\>/,&/i ; ta' *
other seds:
sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta'
4.15. prevent regex expansion on substitutions
4.16. convert a string to all lowercase or capital letters
CHANGING BLOCKS (consecutive lines)
4.20. change only one section of a file
4.21. delete or change a block of text if the block contains a
certain regular expression
4.22. locate a paragraph of text if the paragraph contains a
certain regular expression
4.23. match a block of specific consecutive lines
4.23.1. "/range/, /expression/"
4.23.2. "multi-line\nexpression"
4.23.3. block of "literal strings"
4.24. address all the lines between RE1 and RE2,
excluding the lines themselves
4.25. join two lines if line #1 ends in a [certain string]
4.26. join two lines if line #2 begins in a [certain string]
4.27. change all paragraphs to long lines
|
SHELL AND ENVIRONMENT
4.30. read environment variables with sed ...
4.31.1. on Unix platforms
4.31.2. on DOS platforms
4.32. export or pass variables back
4.32.1. on Unix platforms
4.32.2. on DOS platforms
4.33. handle shell quoting in sed
FILES, DIRECTORIES, AND PATHS
4.40. read (insert/add) a file at the top of a textfile
4.41. make substitutions in every file in a directory, or
in a complete directory tree
4.41.1. ssed solution
4.41.2. Unix solution
4.41.3. DOS solution
4.42. replace "/some/ UNIX/path"
4.43. replace "C:\SOME\DOS\PATH"
4.44. includes
5. WHY ISN'T THIS WORKING?
| 6. OTHER ISSUES
|
5.1. variables don't get expanded
5.2. 'p'outputs duplicate lines sometimes.
5.3. DOS version quits
5.4. RE isn't matching/deleting
(Or, "Greedy vs. stingy pattern matching")
5.5. CSDPMI*B.ZIP
5.6. man pages for GNU sed
5.7. version display
5.8. exit code
5.9. 'r' command isn't inserting the file
5.10. match newline using the \n
5.11. "event not found"
|
6.1. Where to get help
6.2. compare with awk, perl, and other utilities
6.3. When to use sed*
6.4. When NOT to*
6.5. When to use awk or perl instead
6.6. limitations
6.7. incompatibilities
6.7.1. command line
6.7.2. Using comment*
6.7.3. Special syntax in REs
6.7.4. Word boundaries
6.7.5. GNU N*operates differently
|
7. KNOWN BUGS AMONG SED VERSIONS
| | |