Benutzer:Andreas Plank/Sed

Aus Offene Naturführer
Wechseln zu: Navigation, Suche

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'
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:34 von https://offene-naturfuehrer.de/web/Benutzer:Andreas_Plank/Sed