Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
sed [2024/07/19 00:42] admin angelegt |
sed [2025/05/05 14:07] (aktuell) admin |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | See also [[grep]], [[awk|awk]]. | ||
+ | < | ||
+ | # ersetzt das erste Vorkommen von suchmuster in jeder Zeile der Datei | ||
+ | sed ' | ||
+ | |||
+ | # g (global) alle Vorkommen von suchmuster in jeder Zeile ersetzt | ||
+ | sed ' | ||
+ | |||
+ | # nur in der 2. Zeile | ||
+ | sed ' | ||
+ | |||
+ | # nur Zeilen die dem Suchmuster entsprechen | ||
+ | sed '/ | ||
+ | |||
+ | # vor der zweiten Zeile einfügen. a kann verwendet werden, um Text nach der Zeile einzufügen | ||
+ | sed ' | ||
+ | |||
+ | # Lösche die 2. Zeile | ||
+ | sed ' | ||
+ | |||
+ | # Anweisungen kombinieren (gilt für jede Zeile) | ||
+ | sed -e ' | ||
+ | |||
+ | # Multiline Kommentare (/* Multiline Kommentar */) entfernen | ||
+ | sed -i '/ | ||
+ | </ | ||
+ | |||
+ | =====Cheatsheet===== | ||
+ | |||
+ | < | ||
+ | |||
+ | FILE SPACING: | ||
+ | |||
+ | # double space a file | ||
+ | sed G | ||
+ | |||
+ | # double space a file which already has blank lines in it. Output file | ||
+ | # should contain no more than one blank line between lines of text. | ||
+ | sed '/ | ||
+ | |||
+ | # triple space a file | ||
+ | sed ' | ||
+ | |||
+ | # undo double-spacing (assumes even-numbered lines are always blank) | ||
+ | sed ' | ||
+ | |||
+ | # insert a blank line above every line which matches " | ||
+ | sed '/ | ||
+ | |||
+ | # insert a blank line below every line which matches " | ||
+ | sed '/ | ||
+ | |||
+ | # insert a blank line above and below every line which matches " | ||
+ | sed '/ | ||
+ | |||
+ | NUMBERING: | ||
+ | |||
+ | # number each line of a file (simple left alignment). Using a tab (see | ||
+ | # note on ' | ||
+ | sed = filename | sed ' | ||
+ | |||
+ | # number each line of a file (number on left, right-aligned) | ||
+ | sed = filename | sed 'N; s/^/ /; s/ *\(.\{6, | ||
+ | |||
+ | # number each line of file, but only print numbers if line is not blank | ||
+ | sed '/ | ||
+ | |||
+ | # count lines (emulates "wc -l") | ||
+ | sed -n ' | ||
+ | |||
+ | TEXT CONVERSION AND SUBSTITUTION: | ||
+ | |||
+ | # IN UNIX ENVIRONMENT: | ||
+ | sed ' | ||
+ | sed ' | ||
+ | sed ' | ||
+ | |||
+ | # IN UNIX ENVIRONMENT: | ||
+ | sed " | ||
+ | sed ' | ||
+ | sed " | ||
+ | sed ' | ||
+ | |||
+ | # IN DOS ENVIRONMENT: | ||
+ | sed " | ||
+ | sed -n p # method 2 | ||
+ | |||
+ | # IN DOS ENVIRONMENT: | ||
+ | # Can only be done with UnxUtils sed, version 4.0.7 or higher. The | ||
+ | # UnxUtils version can be identified by the custom " | ||
+ | # which appears when you use the " | ||
+ | # DOS newlines to Unix newlines cannot be done with sed in a DOS | ||
+ | # environment. Use " | ||
+ | sed " | ||
+ | tr -d \r <infile > | ||
+ | |||
+ | # delete leading whitespace (spaces, tabs) from front of each line | ||
+ | # aligns all text flush left | ||
+ | sed 's/^[ \t]*//' | ||
+ | |||
+ | # delete trailing whitespace (spaces, tabs) from end of each line | ||
+ | sed 's/[ \t]*$//' | ||
+ | |||
+ | # delete BOTH leading and trailing whitespace from each line | ||
+ | sed 's/^[ \t]*//;s/[ \t]*$//' | ||
+ | |||
+ | # insert 5 blank spaces at beginning of each line (make page offset) | ||
+ | sed ' | ||
+ | |||
+ | # align all text flush right on a 79-column width | ||
+ | sed -e :a -e ' | ||
+ | |||
+ | # center all text in the middle of 79-column width. In method 1, | ||
+ | # spaces at the beginning of the line are significant, | ||
+ | # spaces are appended at the end of the line. In method 2, spaces at | ||
+ | # the beginning of the line are discarded in centering the line, and | ||
+ | # no trailing spaces appear at the end of lines. | ||
+ | | ||
+ | | ||
+ | |||
+ | # substitute (find and replace) " | ||
+ | sed ' | ||
+ | sed ' | ||
+ | sed ' | ||
+ | sed ' | ||
+ | sed ' | ||
+ | |||
+ | # substitute " | ||
+ | sed '/ | ||
+ | |||
+ | # substitute " | ||
+ | sed '/ | ||
+ | |||
+ | # change " | ||
+ | sed ' | ||
+ | gsed ' | ||
+ | |||
+ | # reverse order of lines (emulates " | ||
+ | # bug/feature in HHsed v1.5 causes blank lines to be deleted | ||
+ | sed ' | ||
+ | sed -n ' | ||
+ | |||
+ | # reverse each character on the line (emulates " | ||
+ | sed '/ | ||
+ | |||
+ | # join pairs of lines side-by-side (like " | ||
+ | sed ' | ||
+ | |||
+ | # if a line ends with a backslash, append the next line to it | ||
+ | sed -e :a -e '/ | ||
+ | |||
+ | # if a line begins with an equal sign, append it to the previous line | ||
+ | # and replace the " | ||
+ | sed -e :a -e ' | ||
+ | |||
+ | # add commas to numeric strings, changing " | ||
+ | gsed ': | ||
+ | sed -e :a -e ' | ||
+ | |||
+ | # add commas to numbers with decimal points and minus signs (GNU sed) | ||
+ | gsed -r ': | ||
+ | |||
+ | # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.) | ||
+ | gsed ' | ||
+ | sed ' | ||
+ | |||
+ | SELECTIVE PRINTING OF CERTAIN LINES: | ||
+ | |||
+ | # print first 10 lines of file (emulates behavior of " | ||
+ | sed 10q | ||
+ | |||
+ | # print first line of file (emulates "head -1") | ||
+ | sed q | ||
+ | |||
+ | # print the last 10 lines of a file (emulates " | ||
+ | sed -e :a -e ' | ||
+ | |||
+ | # print the last 2 lines of a file (emulates "tail -2") | ||
+ | sed ' | ||
+ | |||
+ | # print the last line of a file (emulates "tail -1") | ||
+ | sed ' | ||
+ | sed -n ' | ||
+ | |||
+ | # print the next-to-the-last line of a file | ||
+ | sed -e ' | ||
+ | sed -e ' | ||
+ | sed -e ' | ||
+ | |||
+ | # print only lines which match regular expression (emulates " | ||
+ | sed -n '/ | ||
+ | sed '/ | ||
+ | |||
+ | # print only lines which do NOT match regexp (emulates "grep -v") | ||
+ | sed -n '/ | ||
+ | sed '/ | ||
+ | |||
+ | # print the line immediately before a regexp, but not the line | ||
+ | # containing the regexp | ||
+ | sed -n '/ | ||
+ | |||
+ | # print the line immediately after a regexp, but not the line | ||
+ | # containing the regexp | ||
+ | sed -n '/ | ||
+ | |||
+ | # print 1 line of context before and after regexp, with line number | ||
+ | # indicating where the regexp occurred (similar to "grep -A1 -B1") | ||
+ | sed -n -e '/ | ||
+ | |||
+ | # grep for AAA and BBB and CCC (in any order) | ||
+ | sed '/ | ||
+ | |||
+ | # grep for AAA and BBB and CCC (in that order) | ||
+ | sed '/ | ||
+ | |||
+ | # grep for AAA or BBB or CCC (emulates " | ||
+ | sed -e '/ | ||
+ | gsed '/ | ||
+ | |||
+ | # print paragraph if it contains AAA (blank lines separate paragraphs) | ||
+ | # HHsed v1.5 must insert a ' | ||
+ | sed -e '/ | ||
+ | |||
+ | # print paragraph if it contains AAA and BBB and CCC (in any order) | ||
+ | sed -e '/ | ||
+ | |||
+ | # print paragraph if it contains AAA or BBB or CCC | ||
+ | sed -e '/ | ||
+ | gsed '/ | ||
+ | |||
+ | # print only lines of 65 characters or longer | ||
+ | sed -n '/ | ||
+ | |||
+ | # print only lines of less than 65 characters | ||
+ | sed -n '/ | ||
+ | sed '/ | ||
+ | |||
+ | # print section of file from regular expression to end of file | ||
+ | sed -n '/ | ||
+ | |||
+ | # print section of file based on line numbers (lines 8-12, inclusive) | ||
+ | sed -n ' | ||
+ | sed ' | ||
+ | |||
+ | # print line number 52 | ||
+ | sed -n ' | ||
+ | sed ' | ||
+ | sed ' | ||
+ | |||
+ | # beginning at line 3, print every 7th line | ||
+ | gsed -n ' | ||
+ | sed -n ' | ||
+ | |||
+ | # print section of file between two regular expressions (inclusive) | ||
+ | sed -n '/ | ||
+ | |||
+ | SELECTIVE DELETION OF CERTAIN LINES: | ||
+ | |||
+ | # print all of file EXCEPT section between 2 regular expressions | ||
+ | sed '/ | ||
+ | |||
+ | # delete duplicate, consecutive lines from a file (emulates " | ||
+ | # First line in a set of duplicate lines is kept, rest are deleted. | ||
+ | sed '$!N; / | ||
+ | |||
+ | # delete duplicate, nonconsecutive lines from a file. Beware not to | ||
+ | # overflow the buffer size of the hold space, or else use GNU sed. | ||
+ | sed -n 'G; s/ | ||
+ | |||
+ | # delete all lines except duplicate lines (emulates "uniq -d"). | ||
+ | sed '$!N; s/ | ||
+ | |||
+ | # delete the first 10 lines of a file | ||
+ | sed ' | ||
+ | |||
+ | # delete the last line of a file | ||
+ | sed ' | ||
+ | |||
+ | # delete the last 2 lines of a file | ||
+ | sed ' | ||
+ | |||
+ | # delete the last 10 lines of a file | ||
+ | sed -e :a -e ' | ||
+ | sed -n -e :a -e ' | ||
+ | |||
+ | # delete every 8th line | ||
+ | gsed ' | ||
+ | sed ' | ||
+ | |||
+ | # delete lines matching pattern | ||
+ | sed '/ | ||
+ | |||
+ | # delete ALL blank lines from a file (same as "grep ' | ||
+ | sed '/ | ||
+ | sed '/ | ||
+ | |||
+ | # delete all CONSECUTIVE blank lines from file except the first; also | ||
+ | # deletes all blank lines from top and end of file (emulates "cat -s") | ||
+ | sed '/ | ||
+ | sed '/ | ||
+ | |||
+ | # delete all CONSECUTIVE blank lines from file except the first 2: | ||
+ | sed '/ | ||
+ | |||
+ | # delete all leading blank lines at top of file | ||
+ | sed '/ | ||
+ | |||
+ | # delete all trailing blank lines at end of file | ||
+ | sed -e :a -e '/ | ||
+ | sed -e :a -e '/ | ||
+ | |||
+ | # delete the last line of each paragraph | ||
+ | sed -n '/ | ||
+ | |||
+ | SPECIAL APPLICATIONS: | ||
+ | |||
+ | # remove nroff overstrikes (char, backspace) from man pages. The ' | ||
+ | # command may need an -e switch if you use Unix System V or bash shell. | ||
+ | sed " | ||
+ | sed ' | ||
+ | sed ' | ||
+ | |||
+ | # get Usenet/ | ||
+ | sed '/ | ||
+ | |||
+ | # get Usenet/ | ||
+ | sed ' | ||
+ | |||
+ | # get Subject header, but remove initial " | ||
+ | sed '/ | ||
+ | |||
+ | # get return address header | ||
+ | sed '/ | ||
+ | |||
+ | # parse out the address proper. Pulls out the e-mail address by itself | ||
+ | # from the 1-line return address header (see preceding script) | ||
+ | sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//' | ||
+ | |||
+ | # add a leading angle bracket and space to each line (quote a message) | ||
+ | sed ' | ||
+ | |||
+ | # delete leading angle bracket & space from each line (unquote a message) | ||
+ | sed ' | ||
+ | |||
+ | # remove most HTML tags (accommodates multiple-line tags) | ||
+ | sed -e :a -e ' | ||
+ | |||
+ | # extract multi-part uuencoded binaries, removing extraneous header | ||
+ | # info, so that only the uuencoded portion remains. Files passed to | ||
+ | # sed must be passed in the proper order. Version 1 can be entered | ||
+ | # from the command line; version 2 can be made into an executable | ||
+ | # Unix shell script. (Modified from a script by Rahul Dhesi.) | ||
+ | sed '/ | ||
+ | sed '/ | ||
+ | |||
+ | # sort paragraphs of file alphabetically. Paragraphs are separated by blank | ||
+ | # lines. GNU sed uses \v for vertical tab, or any unique char will do. | ||
+ | sed '/ | ||
+ | gsed '/ | ||
+ | |||
+ | # zip up each .TXT file individually, | ||
+ | # setting the name of each .ZIP file to the basename of the .TXT file | ||
+ | # (under DOS: the "dir /b" switch returns bare filenames in all caps). | ||
+ | echo @echo off > | ||
+ | dir /b *.txt | sed " | ||
+ | |||
+ | TYPICAL USE: Sed takes one or more editing commands and applies all of | ||
+ | them, in sequence, to each line of input. After all the commands have | ||
+ | been applied to the first input line, that line is output and a second | ||
+ | input line is taken for processing, and the cycle repeats. The | ||
+ | preceding examples assume that input comes from the standard input | ||
+ | device (i.e, the console, normally this will be piped input). One or | ||
+ | more filenames can be appended to the command line if the input does | ||
+ | not come from stdin. Output is sent to stdout (the screen). Thus: | ||
+ | |||
+ | cat filename | sed ' | ||
+ | sed ' | ||
+ | sed ' | ||
+ | |||
+ | For additional syntax instructions, | ||
+ | commands from a disk file instead of the command line, consult "sed & | ||
+ | awk, 2nd Edition," | ||
+ | 1997; http:// | ||
+ | and Tim O' | ||
+ | distributed in U-SEDIT2.ZIP (many sites). To fully exploit the power | ||
+ | of sed, one must understand " | ||
+ | " | ||
+ | The manual (" | ||
+ | sed", "man regexp", | ||
+ | ed"), but man pages are notoriously difficult. They are not written to | ||
+ | teach sed use or regexps to first-time users, but as a reference text | ||
+ | for those already acquainted with these tools. | ||
+ | |||
+ | QUOTING SYNTAX: The preceding examples use single quotes (' | ||
+ | instead of double quotes (" | ||
+ | sed is typically used on a Unix platform. Single quotes prevent the | ||
+ | Unix shell from intrepreting the dollar sign ($) and backquotes | ||
+ | (`...`), which are expanded by the shell if they are enclosed in | ||
+ | double quotes. Users of the " | ||
+ | to quote the exclamation mark (!) with the backslash (i.e., \!) to | ||
+ | properly run the examples listed above, even within single quotes. | ||
+ | Versions of sed written for DOS invariably require double quotes | ||
+ | (" | ||
+ | |||
+ | USE OF ' | ||
+ | the expression ' | ||
+ | However, most versions of sed do not recognize the ' | ||
+ | so when typing these scripts from the command line, you should press | ||
+ | the TAB key instead. ' | ||
+ | metacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80. | ||
+ | |||
+ | VERSIONS OF SED: Versions of sed do differ, and some slight syntax | ||
+ | variation is to be expected. In particular, most do not support the | ||
+ | use of labels (:name) or branch instructions (b,t) within editing | ||
+ | commands, except at the end of those commands. We have used the syntax | ||
+ | which will be portable to most users of sed, even though the popular | ||
+ | GNU versions of sed allow a more succinct syntax. When the reader sees | ||
+ | a fairly long command such as this: | ||
+ | |||
+ | sed -e '/ | ||
+ | |||
+ | it is heartening to know that GNU sed will let you reduce it to: | ||
+ | |||
+ | sed '/ | ||
+ | sed '/ | ||
+ | |||
+ | In addition, remember that while many versions of sed accept a command | ||
+ | like "/one/ s/ | ||
+ | contains space before the ' | ||
+ | |||
+ | OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to | ||
+ | large input files or slow processors or hard disks), substitution will | ||
+ | be executed more quickly if the " | ||
+ | giving the " | ||
+ | |||
+ | sed ' | ||
+ | sed '/foo/ s/ | ||
+ | sed '/foo/ s// | ||
+ | |||
+ | On line selection or deletion in which you only need to output lines | ||
+ | from the first part of the file, a " | ||
+ | will drastically reduce processing time for large files. Thus: | ||
+ | |||
+ | sed -n ' | ||
+ | sed -n ' | ||
+ | |||
+ | </ |