Sed/Adressierung
Adressierung
- Es ist möglich, bestimmte Zeilen explizit auszuwählen, also zum Beispiel "Zeile 1", "Zeile 1-2", "letzte Zeile" usw.
- In diesem Falle wird die Bereichsauswahl direkt angegeben, sie steht also nicht zwischen "/" wie bei RE-Ausdrücken.
- Der Befehl 'p' steht direkt hinter der Bereichsauswahl.
Using Address and Patterns
- from a file using sed address and patterns.
- “p” command prints the buffer (remember to use -n option with “p”)
- will delete the pattern space buffer and immediately starts the next cycle.
Syntax
sed 'ADDRESS'd filename
sed /PATTERN/d filename
- Syntax for ADDRESSES and PATTERNS given in the printing is applicable for deletion also, except -n option. (-n only to suppress printing pattern buffer, can be used with “p” command )
- Let us first creates thegeekstuff.txt file that will be used in all the examples mentioned below.
# cat thegeekstuff.txt 1. Linux - Sysadmin, Scripting etc. 2. Databases - Oracle, mySQL etc. 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 etc.
ADDRESS Format Beispiele
NUMBER
This will match only Nth line in the input.
# sed -n ‘N’p filename
For example, 3p prints third line of input file thegeekstuff.txt as shown below.
# sed -n '3'p thegeekstuff.txt 3. Hardware
Erste Zeile ausgeben
sed -n '1p' < sed-test.txt
Option "-n" nicht vergessen!
NUMBER1~NUMBER2
M~N with “p” command prints every Nth line starting from line M.
# sed -n ‘M~N’p filename
For example, 3~2p prints every 2nd line starting from 3rd line as shown below.
# sed -n '3~2'p thegeekstuff.txt 3. Hardware 5. Storage 7. Productivity (Too many technologies to explore, not much time available) 9. Software Development
START, END
M,N with “p” command prints Mth line to Nth line.
# sed -n ‘M,N’p filename
For example, 4,8p prints from 4th line to 8th line from input file thegeekstuff.txt
# sed -n '4,8'p thegeekstuff.txt 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
‘$’ Last Line
$ with “p” command matches only the last line from the input.
# sed -n ‘$’p filename
For example, $p prints only the last line as shown below.
# sed -n '$'p thegeekstuff.txt 10.Windows- Sysadmin, reboot etc.
NUMBER,$
N,$ with “p” command prints from Nth line to end of file.
# sed -n ‘N,$p’ filename
For example 4,$p prints from 4th line to end of file.
# sed -n '4,$p' thegeekstuff.txt 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 etc.
Letzte Zeile ausgeben
sed -n '$p' < sed-test.txt
PATTERN Format Beispiele
PATTERN
PATTERN could be unix regular expression.
- The below command prints only the line which matches the given pattern.
# sed -n /PATTERN/p filename
For example, following prints the line only which matches the pattern “Sysadmin”.
# sed -n /Sysadmin/p thegeekstuff.txt 1. Linux - Sysadmin, Scripting etc. 10.Windows- Sysadmin, reboot etc.
/PATTERN/,ADDRESS
# sed -n ‘/PATTERN/,Np’ filename
For example, following prints lines which matches the pattern to Nth line, from input. 3rd line matches the pattern “Hardware”, so it prints from 3rd line to 6th line.
# sed -n '/Hardware/,6p' thegeekstuff.txt 3. Hardware 4. Security (Firewall, Network, Online Security etc) 5. Storage 6. Cool gadgets and websites
ADDRESS,/PATTERN/
- It prints from the Nth line of the input, to the line which matches the pattern.
- If the pattern doesnt match, it prints upto end of the input.
# sed -n ‘N,/PATTERN/p’ filename
For example, 4th line matches the pattern “Security”, so it prints from 3rd line to 4th line.
# sed -n '3,/Security/p' thegeekstuff.txt 3. Hardware 4. Security (Firewall, Network, Online Security etc)
/PATTERN/,$
It prints from the line matches the given pattern to end of file.
# sed -n ‘/PATTERN/,$p’ filename
# sed -n '/Website/,$p' thegeekstuff.txt 8. Website Design 9. Software Development 10.Windows- Sysadmin, reboot etc.
/PATTERN/,+N
It prints the lines which matches the pattern and next N lines following the matched line.
# sed -n ‘/PATTERN/,+Np’ filename
For example, following prints the 5th line which matches the pattern /Storage/ and next two lines following /Storage/.
# sed -n '/Storage/,+2p' thegeekstuff.txt 5. Storage 6. Cool gadgets and websites 7. Productivity (Too many technologies to explore, not much time available)
/PATTERN/,/PATTERN/
Prints the section of file between two regular expression (including the matched line ).
# sed -n ‘/P1/,/P2/p’ filename
For example, 5th line matches “Storage” and 8th line matches “Design”, so it prints 5th to 8th.
# sed -n '/Storage/,/Design/p' thegeekstuff.txt 5. Storage 6. Cool gadgets and websites 7. Productivity (Too many technologies to explore, not much time available) 8. Website Design
Besondere Zeichen
Negation
Das Ausrufezeichen ("!") hinter der Bereichsauswahl bewirkt eine Negation
sed '/isst/!d' < sed-test.txt
- Zeilen, die "isst" nicht enthalten, werden nicht ausgegeben
sed -n '1!p' < sed-test.txt
- alle Zeilen außer Zeile 1 werden ausgegeben
$-Zeichen: letztes Element
Das $-Zeichen steht bei der Bereichsauswahl für "letzte Zeile":
sed -n '$p' < sed-test.txt
- letzte Zeile ausgeben
In einer RE steht das $-Zeichen für "Zeilenende"
sed -n '/Zeile$/p' < sed-test.txt
- Zeilen ausgeben, in denen "Zeile" am Ende steht
Möchte man dem Zeilenende eine Zeichenkette anhängen, genügt ein $ als Suchmuster:
sed -n 's/$/ und Schluss!/p' < sed-test.txt
- Jede Zeile endet mit ' und Schluss!'
^ - Zeichen
Das ^-Zeichen steht in einer RE für Zeilenanfang:
sed -n '/^Hier/p' < sed-test.txt
- Zeilen ausgeben, in denen "Hier" am Anfang steht
Innerhalb einer Zeichenklasse in einer RE steht es für Verneinung.
sed -n '/F[^e]hler/p' < sed-test.txt
- Zeilen ausgeben, in denen das Wort "F.hler" vorkommt, wobei der 2.
- Buchstabe kein "e" sein darf
Möchte man dem Zeilenanfang eine Zeichenkette voranstellen genügt ein ^ als Suchmuster:
sed -n 's/^/Am Anfang stand /p' < sed-test.txt
- Jede Zeile beginnt mit 'Am Anfang stand '