Differences
This shows you the differences between two versions of the page.
linux_wiki:sed [2016/03/18 23:23] billdozor [Sed] |
linux_wiki:sed [2019/05/25 23:50] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Sed ====== | ||
- | **General Information** | ||
- | |||
- | Stream editor tips and tricks. | ||
- | |||
- | **Checklist** | ||
- | * Distro(s): Any | ||
- | * Package: ' | ||
- | |||
- | ---- | ||
- | |||
- | ====== Print Lines ====== | ||
- | |||
- | Different ways to print specific lines. | ||
- | |||
- | Print only line 5 pattern space< | ||
- | * -n => silent; do not print the entire file | ||
- | |||
- | Line 5, don't delete it<code bash>sed ' | ||
- | * !d => don't delete | ||
- | |||
- | Print up to line 5, quit and delete the rest of the output< | ||
- | * q => quit/stop processing more input | ||
- | * d => delete the pattern space (removes all but line 5 from output) | ||
- | |||
- | ---- | ||
- | |||
- | ====== Insert Lines ====== | ||
- | |||
- | Insert lines before a found matched pattern. | ||
- | |||
- | Given the contents of / | ||
- | <code bash> | ||
- | # Generated by NetworkManager | ||
- | nameserver 192.168.1.254 | ||
- | </ | ||
- | |||
- | Insert a new DNS entry that will be checked first: | ||
- | <code bash> | ||
- | sed -i '/ | ||
- | </ | ||
- | * / | ||
- | * -i => in-line editing (changes the file in place) | ||
- | * i\ => insert | ||
- | |||
- | Results in: | ||
- | <code bash> | ||
- | # Generated by NetworkManager | ||
- | nameserver 8.8.8.8 | ||
- | nameserver 192.168.1.254 | ||
- | </ | ||
- | |||
- | ===== Insert at first line ===== | ||
- | |||
- | Insert also works against line numbers. | ||
- | |||
- | Insert a new comment at line 1: | ||
- | <code bash> | ||
- | sed -i '1 i\# My first line comment' | ||
- | </ | ||
- | |||
- | Results in: | ||
- | <code bash> | ||
- | # My first line comment | ||
- | # Generated by NetworkManager | ||
- | nameserver 8.8.8.8 | ||
- | nameserver 192.168.1.254 | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== Append Lines ====== | ||
- | |||
- | Append lines after a found matched pattern. | ||
- | |||
- | Given the contents of / | ||
- | <code bash> | ||
- | # Generated by NetworkManager | ||
- | nameserver 8.8.8.8 | ||
- | nameserver 192.168.1.254 | ||
- | </ | ||
- | |||
- | Append a new DNS entry that will be checked 2nd: | ||
- | <code bash> | ||
- | sed -i '/ | ||
- | </ | ||
- | |||
- | Results in: | ||
- | <code bash> | ||
- | # Generated by NetworkManager | ||
- | nameserver 8.8.8.8 | ||
- | nameserver 8.8.4.4 | ||
- | nameserver 192.168.1.254 | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ====== Remove Lines ====== | ||
- | |||
- | Remove lines that match a pattern. | ||
- | |||
- | Given / | ||
- | <code bash> | ||
- | # Generated by NetworkManager | ||
- | nameserver 8.8.8.8 | ||
- | nameserver 8.8.4.4 | ||
- | nameserver 192.168.1.254 | ||
- | </ | ||
- | |||
- | Remove the 8.8.4.4 entry: | ||
- | <code bash> | ||
- | sed -i '/ | ||
- | </ | ||
- | |||
- | Results in: | ||
- | <code bash> | ||
- | # Generated by NetworkManager | ||
- | nameserver 8.8.8.8 | ||
- | nameserver 192.168.1.254 | ||
- | </ | ||
- | |||
- | ---- |