Tag Archives: shell

Substitute a text only in files where it appears

For example, to substitute foo with bar

for f in `grep -lR foo`; do
	echo -n ">> $f";
	sed 's/foo/bar/g' $f > $f.tmp ;
	mv $f.tmp $f ;
	echo " done.";
done
  • 1) for loops on all files containing the text “foo”, grep -l only show the file name with corresponding text inside.
  • 3) sed replace foo with bar in all file
  • 4) mv save the modification (delete if you don’t want to overwrite the original files, the modified files is named with .tmp at the end)
  • 2) & 5) echo: which file is treated

Convert multiple files to utf8 using vim

The fastest and more efficient way to batch multiple files encoding conversion :

vim +"argdo se fileencoding=utf-8 | w | bnext" +"q" ` find . -type f -name "*.rsp" `

+”argdo” : execute the following vim commands for each file (filencode, save and next buffer)

+”q” : quit after last one

find : get all files with corresponding name