Post

sed

Regular expressions.

  • Replace the first occurrence of a string in a file, and print the result:
    1
    
    sed 's/find/replace/' filename
    
  • Replace only on lines matching the line pattern:
    1
    
    sed '/line_pattern/s/find/replace/'
    
  • Replace all occurrences of a string in a file, overwriting the file (i.e. in-place):
    1
    
    sed -i 's/find/replace/g' filename
    
  • Replace all occurrences of an extended regular expression in a file:
    1
    
    sed -r 's/regex/replace/g' filename
    
  • Apply multiple find-replace expressions to a file:
    1
    
    sed -e 's/find/replace/' -e 's/find/replace/' filename
    

File spacing

  • double space a file
    1
    
    sed G
    
  • double space a file which already has blank lines in it. Output file should contain no more than one blank line between lines of text.
    1
    
    sed '/^$/d;G'
    
  • triple space a file
    1
    
    sed 'G;G'
    
  • undo double-spacing (assumes even-numbered lines are always blank)
    1
    
    sed 'n;d'
    
  • insert a blank line above every line which matches “regex”
    1
    
    sed '/regex/{x;p;x;}'
    
  • insert a blank line below every line which matches “regex”
    1
    
    sed '/regex/G'
    
  • insert a blank line above and below every line which matches “regex”
    1
    
    sed '/regex/{x;p;x;G;}'
    

Userful

  • List out the second column in the table.
    1
    
    cat text/table.txt | sed 1d | awk '{ print $2 }'
    
  • Sum the columns in the table.
    1
    
    cat text/table.txt | sed 1d | awk '{ sum += $2 } END { print sum }'
    
  • Kills all processes by name.
    1
    
    ps aux | grep chrome | awk '{ print $2 }' | kill (or) pkill chrome
    
  • Deletes trailing whitespace.
    1
    
    sed 's/\s\+$//g' filename
    
  • Deletes all blank lines from file.
    1
    
    sed '/^$/d' filename
    
  • Insert ‘use strict’ to the top of every js file.
    1
    
    sed "1i 'use strict';" *.js
    
  • Append a new line at the end of every file.
    1
    
    sed '1a \n' *
    
  • Generate random numbers and then sort.
    1
    
    for i in {1..20}; do echo $(($RANDOM * 777 * $i)); done | sort -n
    
  • Commatize numbers.
    1
    
    sed -r ':loop; s/(.*[0-9])([0-9]{3})/\1,\2/; t loop' text/numbers.txt
    

Sed Print

  • Print contents of a file.
    1
    
    sed -n '/fox/p' text/* (or) sed -n '/Sysadmin/p' text/geek.txt
    
  • Print lines starting with 3 and skipping by 2.
    1
    
    sed -n '3~2p' text/geek.txt
    
  • Print the last line.
    1
    
    sed -n '$p' text/geek.txt
    
  • Prints the lines matching the between the two patterns.
    sed -n '/Hardware/,/Website/p' text/geek.txt
    

    Sed Print Number

  • Prints the line number for all lines in the file.
    1
    
    sed -n '=' filename
    
  • Prints the line number that matches the pattern.
    1
    
    sed -n '/Linux/=' filename
    
  • Prints the line number in range of two patterns (inclusive).
    1
    
    sed -n '/Linux/,/Hardware/=' filename
    
  • Prints the total number of lines.
    1
    
    sed -n '$=' filename
    
  • number each line of a file (simple left alignment). Using a tab (see note on ‘\t’ at end of file) instead of space will preserve margins.
    1
    
    sed = filename | sed 'N;s/\n/\t/'
    
  • number each line of a file (number on left, right-aligned)
    1
    
    sed = filename | sed 'N; s/^/     /; s/ *\(.\{6,\}\)\n/\1  /'
    
  • number each line of file, but only print numbers if line is not blank
    1
    
    sed '/./=' filename | sed '/./N; s/\n/ /'
    
  • count lines (emulates “wc -l”)
    1
    
    sed -n '$='
    

Sed Delete

The d command performs a deletion.

  • Deletes the 3rd line from beginning of file.
    1
    
    sed '3d' text/geek.txt
    
  • Delete every lines starting from 3 and skipping by 2.
    1
    
    sed '3~2d' text/geek.txt
    
  • Delete lines from 3 to 5.
    1
    
    sed '3,5d' text/geek.txt
    
  • Delete the last line.
    1
    
    sed '$d' text/geek.txt
    
  • Delete lines matching the pattern.
    1
    
    sed '/Sysadmin/d' text/geek.txt
    
  • delete lines matching pattern
    1
    
     sed '/pattern/d'
    
  • delete ALL blank lines from a file (same as “grep ‘.’ “)
    1
    2
    
     sed '/^$/d'                           # method 1
     sed '/./!d'                           # method 2
    

    Sed Substitute

    The s command performs a substitution.

  • Simple substituion for the first result.
    1
    
    sed 's/Linux/Unix/' text/geek.txt
    
  • Simple substituion for global instances.
    1
    
    sed 's/Linux/Unix/g' text/geek.txt
    
  • Replace nth instance.
    1
    
    sed 's/Linux/Unix/2' text/geek.txt
    
  • Write matched lines to output.
    1
    
    sed -n 's/Linux/Unix/gp' text/geek.txt > text/geek-sub.txt
    
  • Use regex group for capturing additional patterns (up to 9).
    1
    
    sed 's/\(Linux\).\+/\1/g' text/geek.txt
    
    1
    
    sed -r 's/(Linux).+/\1/g' text/geek.txt
    
  • Remove the last word.
    1
    
    sed -r 's/\d$//g' text/geek.txt
    
  • Remove all letters.
    1
    
    sed -r 's/[a-zA-Z]//g' text/geek.txt
    
  • Remove html tags (WIP).
    1
    
    sed -r 's|(</?[a-z]+>)||g' text/html.txt
    
  • Commatize any number.
    1
    
    sed ':a;s/\B[0-9]\{3\}\>/,&/;ta' text/numbers.txt
    
    1
    
    sed -r ':loop; s/\B[0-9]{3}\>/,&/; t loop' text/numbers.txt
    

Sed Transform

The y command performs a transformation.

  • Converts all lowercase chars to uppercase.
    1
    
    sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' text/geek.txt
    
  • Converts all uppercase chars to lowercase.
    1
    
    sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' text/geek.txt
    
  • Perform a two character shift.
    1
    
    sed 'y/abcdefghijklmnopqrstuvwxyz/cdefghijklmnopqrstuvwxyzab/' text/geek.txt
    

Special appplicatoins

  • get Usenet/e-mail message header
    1
    
    sed '/^$/q'                # deletes everything after first blank line
    
  • get Usenet/e-mail message body
    1
    
    sed '1,/^$/d'              # deletes everything up to first blank line
    
  • get Subject header, but remove initial “Subject: “ portion
    1
    
    sed '/^Subject: */!d; s///;q'
    
  • get return address header
    1
    
    sed '/^Reply-To:/q; /^From:/h; /./d;g;q'
    

Sed Multiple Commands

  • The -e flag allows for multiple commands.
    1
    
    sed -r -e 's/etc\.*//g' -e 's/(\s+)(\))/\2/g' text/geek.tx
    
This post is licensed under CC BY 4.0 by the author.