Sed/Zeilen löschen: Unterschied zwischen den Versionen
Erscheinungsbild
K Textersetzung - „““ durch „"“ |
K Textersetzung - „etc.“ durch „und weitere“ |
||
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt) | |||
Zeile 36: | Zeile 36: | ||
For example, 3d deletes 3rd line and prints other lines as shown below. | For example, 3d deletes 3rd line and prints other lines as shown below. | ||
sed 3d thegeekstuff.txt | sed 3d thegeekstuff.txt | ||
1. Linux - Sysadmin, Scripting | 1. Linux - Sysadmin, Scripting und weitere | ||
2. Databases - Oracle, mySQL | 2. Databases - Oracle, mySQL und weitere | ||
4. Security (Firewall, Network, Online Security etc) | 4. Security (Firewall, Network, Online Security etc) | ||
5. Storage | 5. Storage | ||
Zeile 44: | Zeile 44: | ||
8. Website Design | 8. Website Design | ||
9. Software Development | 9. Software Development | ||
10.Windows- Sysadmin, reboot | 10.Windows- Sysadmin, reboot und weitere | ||
==== Delete Starting from 3rd line and every 2nd line from there. ==== | ==== Delete Starting from 3rd line and every 2nd line from there. ==== | ||
sed '3~2d' thegeekstuff.txt | sed '3~2d' thegeekstuff.txt | ||
1. | 1. | ||
* Linux - Sysadmin, Scripting | * Linux - Sysadmin, Scripting und weitere | ||
2. | 2. | ||
* Databases - Oracle, mySQL | * Databases - Oracle, mySQL und weitere | ||
4. | 4. | ||
* Security (Firewall, Network, Online Security etc) | * Security (Firewall, Network, Online Security etc) | ||
Zeile 58: | Zeile 58: | ||
8. | 8. | ||
* Website Design | * Website Design | ||
10.Windows- Sysadmin, reboot | 10.Windows- Sysadmin, reboot und weitere | ||
==== Delete from 4th to 8th line from file. ==== | ==== Delete from 4th to 8th line from file. ==== | ||
sed '4,8d' thegeekstuff.txt | sed '4,8d' thegeekstuff.txt | ||
1. | 1. | ||
* Linux - Sysadmin, Scripting | * Linux - Sysadmin, Scripting und weitere | ||
2. | 2. | ||
* Databases - Oracle, mySQL | * Databases - Oracle, mySQL und weitere | ||
3. | 3. | ||
* Hardware | * Hardware | ||
9. | 9. | ||
* Software Development | * Software Development | ||
10.Windows- Sysadmin, reboot | 10.Windows- Sysadmin, reboot und weitere | ||
==== Delete the last line from input. ==== | ==== Delete the last line from input. ==== | ||
sed '$d' thegeekstuff.txt | sed '$d' thegeekstuff.txt | ||
1. | 1. | ||
* Linux - Sysadmin, Scripting | * Linux - Sysadmin, Scripting und weitere | ||
2. | 2. | ||
* Databases - Oracle, mySQL | * Databases - Oracle, mySQL und weitere | ||
3. | 3. | ||
* Hardware | * Hardware | ||
Zeile 94: | Zeile 94: | ||
==== Delete the line which matches the given pattern from input. ==== | ==== Delete the line which matches the given pattern from input. ==== | ||
For example, the below command deletes the line which matches with " | For example, the below command deletes the line which matches with "Sysadmin". | ||
sed /Sysadmin/d thegeekstuff.txt | sed /Sysadmin/d thegeekstuff.txt | ||
2. | 2. | ||
* Databases - Oracle, mySQL | * Databases - Oracle, mySQL und weitere | ||
3. | 3. | ||
* Hardware | * Hardware | ||
Zeile 117: | Zeile 117: | ||
sed '/Website/,$d' thegeekstuff.txt | sed '/Website/,$d' thegeekstuff.txt | ||
1. | 1. | ||
* Linux - Sysadmin, Scripting | * Linux - Sysadmin, Scripting und weitere | ||
2. | 2. | ||
* Databases - Oracle, mySQL | * Databases - Oracle, mySQL und weitere | ||
3. | 3. | ||
* Hardware | * Hardware | ||
Zeile 134: | Zeile 134: | ||
sed '/Storage/,+2d' thegeekstuff.txt | sed '/Storage/,+2d' thegeekstuff.txt | ||
1. | 1. | ||
* Linux - Sysadmin, Scripting | * Linux - Sysadmin, Scripting und weitere | ||
2. | 2. | ||
* Databases - Oracle, mySQL | * Databases - Oracle, mySQL und weitere | ||
3. | 3. | ||
* Hardware | * Hardware | ||
Zeile 145: | Zeile 145: | ||
9. | 9. | ||
* Software Development | * Software Development | ||
10.Windows- Sysadmin, reboot | 10.Windows- Sysadmin, reboot und weitere | ||
==== Delete blank Line from a file using sed ==== | ==== Delete blank Line from a file using sed ==== | ||
Zeile 153: | Zeile 153: | ||
sed '/^$/d' thegeekstuff.txt | sed '/^$/d' thegeekstuff.txt | ||
1. | 1. | ||
* Linux - Sysadmin, Scripting | * Linux - Sysadmin, Scripting und weitere | ||
2. | 2. | ||
* Databases - Oracle, mySQL | * Databases - Oracle, mySQL und weitere | ||
3. | 3. | ||
* Hardware | * Hardware | ||
Zeile 170: | Zeile 170: | ||
9. | 9. | ||
* Software Development | * Software Development | ||
10.Windows- Sysadmin, reboot | 10.Windows- Sysadmin, reboot und weitere | ||
[[Kategorie:Sed]] | [[Kategorie:Sed]] |
Aktuelle Version vom 28. April 2025, 10:47 Uhr
Zeilen löschen
Löschen [d]
Der nachfolgende Aufruf löscht alle Zeilen ab der (einschließlich) 4.
- bis zum Dateiende:
sed '4,$d' test.txt 1 Der Aufruf des Stream Editors besitzt immer das Format: 2 3 sed 'Kommando' Dateiname
Das Entfernen aller Zeilen, die mit einem Leerzeichen beginnen, erledigt dieser Aufruf:
sed '/^ /d' test.txt 10 Nummer Genau diese Zeile 11 Start, Ende Alle Zeilen von "Start" bis "Ende" 12 $ Symbolisiert die letzte Zeile 13 RegEx Zeilen, die den Regulären Ausdruck enthalten 14 1, RegEx Von Zeile 1 bis zur ersten Zeile, die RegEx enthält
Der delete-Befehl ist die Umkehrung des print-Befehls: Die bei der Bereichsauswahl (mittels RE oder explizit) selektierten Zeilen werden "gelöscht", also nicht ausgegeben.
sed '/isst/d' < sed-test.txt
- Zeilen, die "isst" enthalten, werden nicht ausgegeben
sed '3d' < sed-test.txt
- die dritte Zeile wird nicht ausgegeben
Beispiele
Lösche die Nte Zeile
‘Nd’ deletes the Nth line and prints the other lines.
sed ‘Nd’ filename
It reads the first line and places in its pattern buffer.
- Check whether supplied command is true for this line, if true, deletes pattern space buffer and starts next cycle. i.e Read next line.
- If supplied command doesnt true, as its normal behaviour it prints the content of the pattern space buffer.
For example, 3d deletes 3rd line and prints other lines as shown below.
sed 3d thegeekstuff.txt 1. Linux - Sysadmin, Scripting und weitere 2. Databases - Oracle, mySQL und weitere 4. Security (Firewall, Network, Online Security etc) 5. Storage 6. Cool gadgets and websites 7. Productivity (Too many technologies to explore, not much time available) 8. Website Design 9. Software Development 10.Windows- Sysadmin, reboot und weitere
Delete Starting from 3rd line and every 2nd line from there.
sed '3~2d' thegeekstuff.txt 1.
- Linux - Sysadmin, Scripting und weitere
2.
- Databases - Oracle, mySQL und weitere
4.
- Security (Firewall, Network, Online Security etc)
6.
- Cool gadgets and websites
8.
- Website Design
10.Windows- Sysadmin, reboot und weitere
Delete from 4th to 8th line from file.
sed '4,8d' thegeekstuff.txt 1.
- Linux - Sysadmin, Scripting und weitere
2.
- Databases - Oracle, mySQL und weitere
3.
- Hardware
9.
- Software Development
10.Windows- Sysadmin, reboot und weitere
Delete the last line from input.
sed '$d' thegeekstuff.txt 1.
- Linux - Sysadmin, Scripting und weitere
2.
- Databases - Oracle, mySQL und weitere
3.
- Hardware
4.
- Security (Firewall, Network, Online Security etc)
5.
- Storage
6.
- Cool gadgets and websites
7.
- Productivity (Too many technologies to explore, not much time available)
8.
- Website Design
9.
- Software Development
Delete the line which matches the given pattern from input.
For example, the below command deletes the line which matches with "Sysadmin".
sed /Sysadmin/d thegeekstuff.txt 2.
- Databases - Oracle, mySQL und weitere
3.
- Hardware
4.
- Security (Firewall, Network, Online Security etc)
5.
- Storage
6.
- Cool gadgets and websites
7.
- Productivity (Too many technologies to explore, not much time available)
8.
- Website Design
9.
- Software Development
Deletes the line from which matches the given pattern to end of the file.
sed '/Website/,$d' thegeekstuff.txt 1.
- Linux - Sysadmin, Scripting und weitere
2.
- Databases - Oracle, mySQL und weitere
3.
- Hardware
4.
- Security (Firewall, Network, Online Security etc)
5.
- Storage
6.
- Cool gadgets and websites
7.
- Productivity (Too many technologies to explore, not much time available)
Deletes the line from which matches the given pattern and 2lines next to that.
sed '/Storage/,+2d' thegeekstuff.txt 1.
- Linux - Sysadmin, Scripting und weitere
2.
- Databases - Oracle, mySQL und weitere
3.
- Hardware
4.
- Security (Firewall, Network, Online Security etc)
8.
- Website Design
9.
- Software Development
10.Windows- Sysadmin, reboot und weitere
Delete blank Line from a file using sed
You can also remove blank lines with sed.
- The following sed example shows how to use sed and remove blank lines.
sed '/^$/d' thegeekstuff.txt 1.
- Linux - Sysadmin, Scripting und weitere
2.
- Databases - Oracle, mySQL und weitere
3.
- Hardware
4.
- Security (Firewall, Network, Online Security etc)
5.
- Storage
6.
- Cool gadgets and websites
7.
- Productivity (Too many technologies to explore, not much time available)
8.
- Website Design
9.
- Software Development
10.Windows- Sysadmin, reboot und weitere