Tools‎ > ‎Ubuntu/Linux server‎ > ‎

Sed

How to replace text in files?

# Replace text in original file: search for 'oldtext', replace with 'newtext', and save to same file.
sed -i 's/oldtext/newtext/g' myfile.txt

  -i  replace in original file
   g  replace all (globally): replace at all position, not only the first where the search text appeared


# use variables: requires double quotes "s/old/new/g"
FILENAME=myfile.txt
SEARCH_FOR=oldtext
REPLACE_WITH=newtext
sed -i "s/${SEARCH_FOR}/${REPLACE_WITH}/g" ${FILENAME}


# use in a pipe
cat myfile.txt | sed 's/oldtext/newtext/g' > newfile.txt




# replacing text without interpreting special characters
# Example: replace old long taxa with short taxa name in data_abundance_table.txt file
OLDTAXA='k__Bacteria;p__Bacteroidetes;c__Bacteroidia;o__Bacteroidales;f__[Paraprevotellaceae];g__[Prevotella]'
NEWTAXA='Prevotella'
# mask/cover special characters: replace [  with \[
OLDTAXA=$(sed -e 's/[]\/ ()$*.^|[]/\\&/g' <<< "${OLDTAXA}")
NEWTAXA=$(sed -e 's/[]\/ ()$*.^|[]/\\&/g' <<< "${NEWTAXA}")
echo ${OLDTAXA} ${NEWTAXA}
# replace taxa names in same file
sed -i "s/${OLDTAXA}/${NEWTAXA}/g" data_abundance_table.txt


# replace multiple taxa present in a two column text file: taxa_old_new_name.txt
cat taxa_old_new_name.txt | while read OLDTAXA NEWTAXA; do
  echo ${OLDTAXA} ${NEWTAXA} ;
  OLDTAXA=$(sed -e 's/[]\/ ()$*.^|[]/\\&/g' <<< "${OLDTAXA}") ;
  NEWTAXA=$(sed -e 's/[]\/ ()$*.^|[]/\\&/g' <<< "${NEWTAXA}") ;
  sed -i "s/${OLDTAXA}/${NEWTAXA}/g" data_abundance_table.txt ;
done