linux_wiki:sed

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

linux_wiki:sed [2015/09/09 22:38]
billdozor [Insert Lines]
linux_wiki:sed [2019/05/25 23:50]
Line 1: Line 1:
-====== Sed ====== 
  
-**General Information** 
- 
-Stream editor tips and tricks.  
- 
-**Checklist** 
-  * Linux distro with the 'sed' package installed 
- 
----- 
- 
-===== Insert Lines ===== 
- 
-Insert lines before a found matched pattern. 
- 
-Given the contents of /etc/resolv.conf: 
-<code bash> 
-# Generated by NetworkManager 
-nameserver 192.168.1.254 
-</code> 
- 
-Insert a new DNS entry that will be checked first: 
-<code bash> 
-sed -i '/nameserver/ i\nameserver 8.8.8.8' /etc/resolv.conf 
-</code> 
-  * /nameserver/ => pattern to match 
-  * -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 
-</code> 
- 
-==== 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' /etc/resolv.conf 
-</code> 
- 
-Results in: 
-<code bash> 
-# My first line comment 
-# Generated by NetworkManager 
-nameserver 8.8.8.8 
-nameserver 192.168.1.254 
-</code> 
- 
----- 
- 
-===== Append Lines ===== 
- 
-Append lines after a found matched pattern. 
- 
-Given the contents of /etc/resolv.conf: 
-<code bash> 
-# Generated by NetworkManager 
-nameserver 8.8.8.8 
-nameserver 192.168.1.254 
-</code> 
- 
-Append a new DNS entry that will be checked 2nd: 
-<code bash> 
-sed -i '/nameserver 8.8.8.8/ a\nameserver 8.8.4.4' /etc/resolv.conf 
-</code> 
- 
-Results in: 
-<code bash> 
-# Generated by NetworkManager 
-nameserver 8.8.8.8 
-nameserver 8.8.4.4 
-nameserver 192.168.1.254 
-</code> 
- 
----- 
- 
-===== Remove Lines ===== 
- 
-Remove lines that match a pattern. 
- 
-Given /etc/resolv.conf: 
-<code bash> 
-# Generated by NetworkManager 
-nameserver 8.8.8.8 
-nameserver 8.8.4.4 
-nameserver 192.168.1.254 
-</code> 
- 
-Remove the 8.8.4.4 entry: 
-<code bash> 
-sed i '/8.8.4.4/d' /etc/resolv.conf 
-</code> 
- 
-Results in: 
-<code bash> 
-# Generated by NetworkManager 
-nameserver 8.8.8.8 
-nameserver 192.168.1.254 
-</code> 
- 
----- 
  • linux_wiki/sed.txt
  • Last modified: 2019/05/25 23:50
  • (external edit)