Benutzer:Andreas Plank/Sed: Unterschied zwischen den Versionen
Aus Offene Naturführer
K (+sed Ersetzungen mit MediaWiki-XML-Export) |
K (→Ersetzungen mit MediaWiki-XML-Export) |
||
Zeile 66: | Zeile 66: | ||
<span style='color:#888786;'># [..] → [..] character definition range</span> | <span style='color:#888786;'># [..] → [..] character definition range</span> | ||
<span style='color:#888786;'># {..} → \{..\}</span> | <span style='color:#888786;'># {..} → \{..\}</span> | ||
− | <span style='color:#888786;'># s → search, g → global scope</span> | + | |
+ | <span style='color:#888786;'>#### single line matches</span> | ||
+ | <span style='color:#888786;'># s → search, g → global scope (multiple replacements)</span> | ||
<span style='color:#888786;'># s/search/replace/g</span> | <span style='color:#888786;'># s/search/replace/g</span> | ||
− | <span style='color:#888786;'># | + | |
− | + | <span style='color:#888786;'>#### match with address</span> | |
− | + | ||
<span style='color:#888786;'># /address/s/search/replace/g</span> | <span style='color:#888786;'># /address/s/search/replace/g</span> | ||
<span style='color:#888786;'># NOT-match or everything except “address”</span> | <span style='color:#888786;'># NOT-match or everything except “address”</span> | ||
<span style='color:#888786;'># /address/!s/search/replace/g</span> | <span style='color:#888786;'># /address/!s/search/replace/g</span> | ||
− | <span style='color:#888786;'>####################### | + | |
+ | <span style='color:#888786;'>#### match with newline (N appends \n to the pattern)</span> | ||
+ | <span style='color:#888786;'># /searchpattern line 1/N;/searchpattern line 2/{</span> | ||
+ | <span style='color:#888786;'># # some thing ONLY with the found pattern!</span> | ||
+ | <span style='color:#888786;'># s/search/replace/g</span> | ||
+ | <span style='color:#888786;'># }</span> | ||
+ | |||
+ | <span style='color:#888786;'>######### MediaWiki Export ##########</span> | ||
<span style='color:#888786;'># don't replace!!</span> | <span style='color:#888786;'># don't replace!!</span> | ||
<span style='color:#888786;'># &amp;nbsp; → &nbps;</span> | <span style='color:#888786;'># &amp;nbsp; → &nbps;</span> | ||
<span style='color:#888786;'># &lt;br /&gt; → <br/></span> | <span style='color:#888786;'># &lt;br /&gt; → <br/></span> | ||
− | <span style='color:#888786;'> | + | <span style='color:#888786;'>#####################################</span> |
<span style='color:#888786;'># set username</span> | <span style='color:#888786;'># set username</span> |
Version vom 8. September 2011, 10:26 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 → change: 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'
Ersetzungen mit MediaWiki-XML-Export
# sed -i -f this_sed_file.sed LiteraturXML-Export.xml # # note the different (default) regexpr !!! # summarised: + is + ? is ? ( is ( { is { → all no expressions! # (..) → \(\) reference # ? → \? 0 or 1 # .+ → .\+ 1 or many # .* → .* 0 or many # [..] → [..] character definition range # {..} → \{..\}
#### single line matches # s → search, g → global scope (multiple replacements) # s/search/replace/g
#### match with address # /address/s/search/replace/g # NOT-match or everything except “address” # /address/!s/search/replace/g
#### match with newline (N appends \n to the pattern) # /searchpattern line 1/N;/searchpattern line 2/{ # # some thing ONLY with the found pattern! # s/search/replace/g # }
######### MediaWiki Export ########## # don't replace!! # &nbsp; → &nbps; # <br /> → <br/> ##################################### # set username # syntax explained # first line and append \n and second line #┌───────────┴───────────────────┐ ↓ ┌───────────┴──────────────┐ /\(<username>\).\+\(<\/username>\)/N;/\(.\+<id>\)[0-9]\+\(<\/id>\)/{ # do something with the found pattern # replace user name and user ID now with "\n" s@\(<username>\).\+\(</username>\n.\+<id>\)[0-9]\+\(</id>\)@\1Andreas Plank\2152\3@g } # comment s@\(<comment>\).\+\(</comment>\)@\1alle Kommas in Semikolon (Stichworte, Ort)\2@g # delete revision /\(<revision>\)/N;/\( \+<id>\)[0-9]\+\(<\/id>\)/{ s@\(<revision>\n \+<id>\)[0-9]\+\(<\/id>\)@\1\2@g } # timestamp (-2 hours) s@\(<timestamp>\).\+\(</timestamp>\)@\12011-09-08T10:12:11Z\2@g # do your replacements here /^|\(Stichworte\|Ort\) *=.\+$/{ s@,@;@g }