Benutzer:Andreas Plank/Sed: Unterschied zwischen den Versionen

Aus Offene Naturführer
Wechseln zu: Navigation, Suche
K
K
Zeile 54: Zeile 54:
 
</source>
 
</source>
  
== Ersetzungen mit MediaWiki-XML-Export==
+
== Sed help ==
  
  <span style='color:#888786;'># sed -i -f this_sed_file.sed LiteraturXML-Export.xml</span>
 
  <span style='color:#888786;'>#</span>
 
  <span style='color:#888786;'># note the different (default) regexpr !!!</span>
 
  <span style='color:#888786;'># summarised: + is + ? is ? ( is ( { is { → all no expressions!</span>
 
  <span style='color:#888786;'>#  (..) → \(\)    reference </span>
 
  <span style='color:#888786;'>#  ?    → \?      0 or 1</span>
 
  <span style='color:#888786;'>#  .+  → .\+    1 or many</span>
 
  <span style='color:#888786;'>#  .*  → .*      0 or many</span>
 
  <span style='color:#888786;'>#  [..] → [..]    character definition range</span>
 
  <span style='color:#888786;'>#  {..} → \{..\}</span>
 
  
   <span style='color:#888786;'>#### single line matches</span>
+
<span style='color:#888786;'>### Command line syntax ###############</span>
   <span style='color:#888786;'># s search, g → global scope (multiple replacements)</span>
+
<span style='color:#888786;'>#   file ↘</span>
   <span style='color:#888786;'># s/search/replace/g</span>
+
<span style='color:#888786;'>#   sed -f sed_replacements.sed old_file.txt &gt; new_file.txt</span>
 +
<span style='color:#888786;'># insert ↘    ↙ file</span>
 +
<span style='color:#888786;'>#    sed -i -f sed_replacements.sed overwritten.txt</span>
 +
 +
<span style='color:#888786;'>### Regular expressions ###############</span>
 +
<span style='color:#888786;'># note the different (default) regexpr !!!</span>
 +
<span style='color:#888786;'># summarised: + is + ? is ? ( is ( { is { | is | → all no expressions!</span>
 +
<span style='color:#888786;'>#   (..) → \(\)    reference </span>
 +
<span style='color:#888786;'>#   ?    \?      0 or 1</span>
 +
<span style='color:#888786;'>#  .+  .\+    1 or many</span>
 +
<span style='color:#888786;'>#  .*  → .*      0 or many</span>
 +
<span style='color:#888786;'>#  [..] → [..]    character definition range</span>
 +
<span style='color:#888786;'>#  {..} → \{..\}</span>
 +
<span style='color:#888786;'>#  | → \| means “or”</span>
 +
 +
<span style='color:#888786;'>### Seachr and replace ################</span>
 +
<span style='color:#888786;'>#  ↙ search          ↙ global scope</span>
 +
<span style='color:#888786;'># s/search/replace/g</span>
 +
 +
<span style='color:#888786;'>### Search and replace (address) ######</span>
 +
<span style='color:#888786;'>#   /address/s/search/replace/g</span>
 +
<span style='color:#888786;'># NOT-matched or everything except “address”</span>
 +
<span style='color:#888786;'>#  /address/!s/search/replace/g</span>
 +
<span style='color:#888786;'># address can be: 1,$ (1st line to the end) or a seach pattern</span>
 +
 +
<span style='color:#888786;'>### multiline search with “address” ###</span>
 +
<span style='color:#888786;'>#              “address”</span>
 +
<span style='color:#888786;'>#┌─────────────────┴─────────────────────┐</span>
 +
<span style='color:#888786;'>#  first line  append \n  second line</span>
 +
<span style='color:#888786;'>#┌───────┴────────┐ ↓  ┌────────┴────────┐ </span>
 +
<span style='color:#0000ff;'>/</span><span style='color:#8f6a32;'>first line pattern</span><span style='color:#0000ff;'>/</span><b>N</b>;<span style='color:#0000ff;'>/</span><span style='color:#8f6a32;'>second line pattern</span><span style='color:#0000ff;'>/</span>{
 +
  <span style='color:#888786;'># do something with the found pattern</span>
 +
  <span style='color:#888786;'>#    search                replace      global scope</span>
 +
  <span style='color:#888786;'>#      ↓                      ↓          ↓</span>
 +
  <b>s</b><span style='color:#0000ff;'>@</span><span style='color:#8f6a32;'>first line</span><span style='color:#ff80e0;'>\n</span><span style='color:#8f6a32;'>second line</span><span style='color:#0000ff;'>@</span><span style='color:#9c0f0f;'>replace pattern</span><span style='color:#0000ff;'>@</span><span style='color:#0057ae;'>g</span>
 +
}
  
  <span style='color:#888786;'>#### match with address</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;'>#  /address/!s/search/replace/g</span>
 
  
  <span style='color:#888786;'>#### match with newline (N appends \n to the pattern)</span>
+
== Ersetzungen mit MediaWiki-XML-Export==
  <span style='color:#888786;'>#  /searchpattern line 1/N;/searchpattern line 2/{</span>
+
 
  <span style='color:#888786;'>#    # some thing ONLY with the found pattern!</span>
+
Siehe [[#Sed_help|Sed help]].
  <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;'>######### MediaWiki Export ##########</span>

Version vom 14. September 2011, 08:51 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'

Sed help

### Command line syntax ###############
#   file ↘
#    sed -f sed_replacements.sed old_file.txt > new_file.txt
# insert ↘    ↙ file
#    sed -i -f sed_replacements.sed overwritten.txt

### Regular expressions ###############
# note the different (default) regexpr !!!
# summarised: + is + ? is ? ( is ( { is { | is | → all no expressions!
#   (..) → \(\)    reference 
#   ?    → \?      0 or 1
#   .+   → .\+     1 or many
#   .*   → .*      0 or many
#   [..] → [..]    character definition range
#   {..} → \{..\}
#   | → \| means “or”

### Seachr and replace ################
#  ↙ search          ↙ global scope
# s/search/replace/g

### Search and replace (address) ######
#   /address/s/search/replace/g
# NOT-matched or everything except “address”
#   /address/!s/search/replace/g
# address can be: 1,$ (1st line to the end) or a seach pattern

### multiline search with “address” ###
#               “address”
#┌─────────────────┴─────────────────────┐
#   first line   append \n  second line
#┌───────┴────────┐ ↓  ┌────────┴────────┐ 
/first line pattern/N;/second line pattern/{
  # do something with the found pattern
  #     search                replace       global scope
  #       ↓                      ↓          ↓
  s@first line\nsecond line@replace pattern@g
}


Ersetzungen mit MediaWiki-XML-Export

Siehe Sed help.

 ######### MediaWiki Export ##########
 #  don't replace!!
 #  &amp;nbsp; → &nbps;
 #  &lt;br /&gt; → <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>\)@\1Ihr Name\2123\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
 }
Quelle: Offene Naturführer, Das Wiki zu Bestimmungsfragen: Benutzer:Andreas Plank/Sed (Zuletzt geändert:
Dieses Attribut ist ein Spezialattribut in diesem Wiki.
23 November 2020 12:09:34). Abgerufen am 23. Dezember 2024, 10:39 von https://offene-naturfuehrer.de/web/Benutzer:Andreas_Plank/Sed