linux_wiki:sed

Differences

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

Link to this comparison view

Next revision
Previous revision
linux_wiki:sed [2015/09/09 22:34]
billdozor created
linux_wiki:sed [2019/05/25 23:50] (current)
Line 6: Line 6:
  
 **Checklist** **Checklist**
-  * Linux distro with the 'sed' package installed+  * Distro(s): Any 
 +  * Package: 'sed' package installed
  
 ---- ----
  
-===== Insert Lines =====+====== Print Lines ====== 
 + 
 +Different ways to print specific lines. 
 + 
 +Print only line 5 pattern space<code bash>sed -n '5p' /etc/passwd</code> 
 +  * -n => silent; do not print the entire file  
 + 
 +Line 5, don't delete it<code bash>sed '5!d' /etc/passwd</code> 
 +  * !d => don't delete 
 + 
 +Print up to line 5, quit and delete the rest of the output<code bash>sed '5q;d' /etc/passwd</code> 
 +  * q => quit/stop processing more input 
 +  * d => delete the pattern space (removes all but line 5 from output) 
 + 
 +---- 
 + 
 +====== Swap Patterns ====== 
 + 
 +Edit files by swapping pattern spaces. 
 + 
 +  * Before<code bash>cat /etc/resolv.conf  
 +# Generated by NetworkManager 
 +search local 
 +nameserver 208.67.222.222 
 +nameserver 208.67.220.220</code> 
 +  * After<code bash>sed -i 's/208.67.222.222/8.8.8.8/g' /etc/resolv.conf  
 +# Generated by NetworkManager 
 +search local 
 +nameserver 8.8.8.8 
 +nameserver 208.67.220.220</code> 
 +    * -i => In-line editing 
 +    * s => swap  
 +    * g => global swap (swap every match, not just the first one) 
 + 
 +---- 
 + 
 +====== Insert Lines ======
  
 Insert lines before a found matched pattern. Insert lines before a found matched pattern.
Line 30: Line 67:
 Results in: Results in:
 <code bash> <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 # Generated by NetworkManager
 nameserver 8.8.8.8 nameserver 8.8.8.8
Line 37: Line 91:
 ---- ----
  
-===== Append Lines =====+====== Append Lines ======
  
 Append lines after a found matched pattern. Append lines after a found matched pattern.
Line 63: Line 117:
 ---- ----
  
-===== Remove Lines =====+====== Remove Lines ======
  
 Remove lines that match a pattern. Remove lines that match a pattern.
Line 77: Line 131:
 Remove the 8.8.4.4 entry: Remove the 8.8.4.4 entry:
 <code bash> <code bash>
-sed i '/8.8.4.4/d' /etc/resolv.conf+sed -i '/8.8.4.4/d' /etc/resolv.conf
 </code> </code>
  
  • linux_wiki/sed.1441852474.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)