15 examples for sed

{{ score }}
  # Replaces all instances of FOO piped through stdin with BAR
sed 's/FOO/BAR/g'
        
{{ score }}
  # search and replace, inplace 
sed -i 's/typo/what_you_meant/' list of files
        
{{ score }}
  # strip trailing whitespace from a file
sed -i 's/\s\+$//' [file]
        
{{ score }}
  # Replace all 'dog' with 'cat' in file.txt, and keep the original in file.txt.orig
sed -i.orig s/dog/cat/g file.txt
        
{{ score }}
  # print all lines from production.log matching the word "error", plus 1 additional line
sed -n '/error/,+1p' production.log
        
{{ score }}
  # remove blank lines from from a text file 
sed '/^[[:space:]]*$/d' input.txt > output.txt
        
{{ score }}
  # print the lines from somefile.txt matching the word "fromText" to the first line that matches "toText"
sed -n '/fromText/ , /toText/p' somefile.txt
        
{{ score }}
  # Find and replace all instances of FOO with BAR in all matching files.
find . -type f -print | xargs sed -i 's/FOO/BAR/g'
        
{{ score }}
  # Remove bad ssh-rsa key
sed -i 19d .ssh/known_hosts
        
{{ score }}
  # delete all lines that contain "foo" inplace
sed -i '/foo/d' $file
        
{{ score }}
  # search and replace, inplace ** forward slashes **
sed -i 's|~|/home/user|' list of files
        
{{ score }}
  # remove multiple blank (empty) lines from file and stores result in new file
sed '/^\s*$/d' input > output
        
{{ score }}
  # Convierte la primera letra de una lista de caracteres en mayuscula
cat filename | sed 's/./\u&/'

# Salida
     # Juan 
     # Diego
     # Ernesto
     # Gerardo
     # David
        
{{ score }}
  # 		alternative to head -5 
      
{{ score }}
  # Strip HTML
sed -e 's/<[^>]*>//g' index.html