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 inside original file, not creating a new file
g replace all (globally): replace at all positions, not only the first where the search text appeared
# using variables: requires double quotes "s/oldtext/newtext/g"
FILENAME=myfile.txt
SEARCH_FOR=oldtext
REPLACE_WITH=newtext
sed -i "s/${SEARCH_FOR}/${REPLACE_WITH}/g" ${FILENAME}
# using a unix-pipe
cat myfile.txt | sed 's/oldtext/newtext/g' > newfile.txt
Replace special characters
# replacing text without interpreting special characters
# Example: replace old long taxa-IDs with short taxa names in taxa_abundance_table.csv file
# get variables containing the search/replace text strings
OLDTAXA='k__Bacteria;p__Bacteroidetes;c__Bacteroidia;o__Bacteroidales;f__[Paraprevotellaceae];g__[Prevotella]'
NEWTAXA='Prevotella'
# mask/cover all special characters: especially replace [ with \[
OLDTAXA=$(sed -e 's/[]\/ ()$*.^|[]/\\&/g' <<< "${OLDTAXA}")
NEWTAXA=$(sed -e 's/[]\/ ()$*.^|[]/\\&/g' <<< "${NEWTAXA}")
echo ${OLDTAXA} ${NEWTAXA}
# replace taxa names in file: taxa_abundance_table.csv
sed -i "s/${OLDTAXA}/${NEWTAXA}/g" taxa_abundance_table.csv
# replace multiple taxa-IDs provided in a two column text file: taxa_old_new_names.txt
cat taxa_old_new_names.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" taxa_abundance_table.csv ;
done
See also
# replace tabs "\t" with newlines "\n"
awk -vRS="\t" -vORS="\n" '1' mytable.csv
# replace newline \n with comma ","
paste -s -d "," myList.txt