Benutzer:Andreas Plank/Sed: Unterschied zwischen den Versionen
Aus Offene Naturführer
K (Die Seite wurde neu angelegt: „Text snippets for the <abbr title="Stream EDitor">sed</abbr> running under Linux. <source lang="bash"> #### file options # -e execute # -f file: script file # -i …“) |
K |
||
Zeile 27: | Zeile 27: | ||
+ | #### addresses for instance with p → print | ||
+ | '1,10p' # line 1 to 10 | ||
+ | '/beginRE/,/endRE/p' # reg. expr: beginRE to endRE | ||
+ | '10~2p' # at line 10 then each 2nd line | ||
+ | '$=' # last line “$” and provide “=” the line | ||
#### examples | #### examples | ||
# sorted lines → delete duplicate lines | # sorted lines → delete duplicate lines | ||
Zeile 33: | Zeile 38: | ||
sed -e '1,10d' # line 1-10 | sed -e '1,10d' # line 1-10 | ||
sed -e '11,$d' # line 11 to end of file | sed -e '11,$d' # line 11 to end of file | ||
+ | sed -e '10~2d' # delete every 2nd line starting from 10 | ||
# extract: | # extract: | ||
sed -n -e '1,10p' | sed -n -e '1,10p' | ||
Zeile 43: | Zeile 49: | ||
# delete lines with debug + print lines with foo | # delete lines with debug + print lines with foo | ||
sed -n -e '/debug/d' -e '/foo/p' | sed -n -e '/debug/d' -e '/foo/p' | ||
+ | |||
+ | # pipes | ||
+ | gcc sourcefile.c 2>&1 | sed -n -e '/warning:/,/error:/p' | ||
</source> | </source> |
Version vom 26. November 2010, 18:24 Uhr
Text snippets for the sed running under Linux.
#### file options
# -e execute
# -f file: script file
# -i insert into file: edit file in place
# -l 40 specify the desired line-wrap length for the “l” command
# -n nothing i.e. quiet
# -r extended regular expressions
# -s separate: consider files as separate
# -u unbuffered
#### actions
# a → append action (after)
# $a append after last line
# c → You can replace the current line with the ‘c’ action
# d → delete action
# i → insert action (before)
# 1i insert before 1st line
# n → ?read the next line
# p → print action
# q → quit immediately without further processing
#### ACTIONS
# D → delete up to the first embedded newline in the pattern space
# N → ?append the next line
#### addresses for instance with p → print
'1,10p' # line 1 to 10
'/beginRE/,/endRE/p' # reg. expr: beginRE to endRE
'10~2p' # at line 10 then each 2nd line
'$=' # last line “$” and provide “=” the line
#### examples
# sorted lines → delete duplicate lines
sed '$!N; /^\(.*\)\n\1$/!P; D' temp2.txt > temp3.txt
# delete:
sed -e '1,10d' # line 1-10
sed -e '11,$d' # line 11 to end of file
sed -e '10~2d' # delete every 2nd line starting from 10
# extract:
sed -n -e '1,10p'
# quit
sed -e '10q' # quit
# commands on multiple lines or with -e again:
sed -e '1,4d
6,9d'
sed -e '1,4d' -e '6,9d'
# delete lines with debug + print lines with foo
sed -n -e '/debug/d' -e '/foo/p'
# pipes
gcc sourcefile.c 2>&1 | sed -n -e '/warning:/,/error:/p'