Sed

SED Onliner

ZEILEN-ABSTÄNDE: # Doppelter Zeilenvorschub sed G # Doppelter Zeilenabstand für Dateien, die Leerzeilen enthalten. # Die Ausgabe sollte keine zwei aufeinander folgenden Leerzeilen enthalten. sed '/^$/d;G' # Dreifacher Zeilenvorschub sed 'G;G' # Doppelter Zeilenvorschub rückgängig machen # (Annahme: jede zweite Zeile ist leer) sed 'n;d' # Füge eine Leerzeile über jeder Zeile ein, die "regex" enthält sed '/regex/{x;p;x;}' # Füge eine Leerzeile unter jeder Zeile ein, die "regex" enthält sed '/regex/G' # Füge eine Leerzeile über und unter jeder Zeile ein, die "regex" enthält sed '/regex/{x;p;x;G;}' Nummerierung # Nummeriere alle Zeilen (linksbündig). Der Tabulator anstelle von Leerzeichen # erhält den Rand. (siehe auch die Bemerkung zu '\\t' am Ende dieser Datei) sed = filename | sed 'N;s/\\n/\\t/' # Nummeriere alle Zeilen (Zahl rechtsbündig in linker Spalte) sed = filename | sed 'N; s/^/ /; s/ \*\\(.\\{6,\\}\\)\\n/\\1 /' # Nummeriere alle Zeilen, aber die Nummern von Leerzeilen werden nicht ausgegeben. sed '/./=' filename | sed '/./N; s/\\n/ /' # Zeilen zählen (Nachahmung von "wc -l") sed -n '$=' TEXT UMWANDLUNG UND ERSETZUNG: # IN EINER UNIX UMGEBUNG: Wandle DOS Zeilenvorschübe (CR/LF) in das Unix-Format. sed 's/.$//' # Annahme: Alle Zeilen enden mit CR/LF sed 's/^M$//' # Bei bash/tcsh: Ctrl-V dann Ctrl-M sed 's/\\x0D$//' # für ssed, gsed 3.

Continue reading

Unix Sed Tutorial: Advanced Sed Substitution Examples

Sed Substitution Delimiter As we discussed in our previous post, we can use the different delimiters such as @ % | ; : in sed substitute command. Let us first create path.txt file that will be used in all the Beispielen mentioned below. cat path.txt /usr/kbos/bin:/usr/local/bin:/usr/jbin:/usr/bin:/usr/sas/bin /usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin:/opt/omni/bin: /opt/omni/lbin:/opt/omni/sbin:/root/bin Beispiel 1 sed @ delimiter: Substitute /opt/omni/lbin to /opt/tools/bin When you substitute a path name which has ‘/’, you can use @ as a delimiter instead of ‘/’. In the sed Beispiel below, in the last line of the input file, /opt/omni/lbin was changed to /opt/tools/bin. sed 's@/opt/omni/lbin@/opt/tools/bin@g' path.txt /usr/kbos/bin:/usr/local/bin:/usr/jbin/:/usr/bin:/usr/sas/bin /usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin:/opt/omni/bin: /opt/tools/bin:/opt/omni/sbin:/root/bin Beispiel 2 sed / delimiter: Substitute /opt/omni/lbin to /opt/tools/bin When you should use ‘/’ in path name related substitution, you have to escape ‘/’ in the substitution data as shown below. In this sed Beispiel, the delimiter ‘/’ was escaped in the REGEXP and REPLACEMENT part. sed 's/\\/opt\\/omni\\/lbin/\\/opt\\/tools\\/bin/g' path.txt /usr/kbos/bin:/usr/local/bin:/usr/jbin/:/usr/bin:/usr/sas/bin /usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin:/opt/omni/bin: /opt/tools/bin:/opt/omni/sbin:/root/bin Sed ‘&’ Get Matched String The precise part of an input line on which the Regular Expression matches is represented by &, which can then be used in the replacement part. Beispiel 1 sed & Usage: Substitute /usr/bin/ to /usr/bin/local sed 's@/usr/bin@&/local@g' path.txt /usr/kbos/bin:/usr/local/bin:/usr/jbin/:/usr/bin/local:/usr/sas/bin /usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin/local:/opt/omni/bin: /opt/omni/lbin:/opt/omni/sbin:/root/bin In the above Beispiel ‘&’ in the replacement part will replace with /usr/bin which is matched pattern and add it with /local.

Continue reading