sed
-
sed 's/Mark Monre/Marc Monroe/' 1.txt > 2.txt
-
find ./* -type f -exec sed -i 's///g' {} \;
The “replace” command
- Syntax:
replace OLD-STRING NEW-STRING OUTPUT-FILE
- Example:
$ replace UNIX Linux newfile
- Example:
$ cat /etc/passwd | replace : '|'
- Partial support for regular expressions: \^ – matches start of line, and $ matches end of line.
- Example: replace all IP address 192.168.1.2 start of line:
$ replace \^192.168.1.2 192.168.5.10 newfile
- a bash script, ‘fixer.sh’
#!/bin/bash replace CHANGEFROM CHANGETO $1.tmp rm $1 mv $1.tmp $1
now run this command line:
$ grep CHANGEFROM |cut -d':' -f1 |xargs -n 1 fixer.sh
the results is that all files in the directory (or whatever you grep for) will be changed automagically.
just make sure the grep doesn’t include the fixer script itself, or it will die half-way through changing when execute permissions are reset!
Perl
- Perl Pie:
perl -p -i -e ’s/hello/goodbye/g’ textfile.txt
- http://www.debian-administration.org/articles/298 has a fine article and discussion on Perl Pie.
-
perl -p -i -e ’s/|00000000.00|/||/g’ myfile.txt
Sources:
- How do I replace text string in many files at once? – http://www.cyberciti.biz/tips/how-do-i-replace-text-string-in-many-files-at-once.html
Related: Regular expressions – https://eikonal.wordpress.com/2010/04/02/regular-expressions/ | Perl online – https://eikonal.wordpress.com/2010/02/15/perl-online/