Even though sed
is commonly used for these types of tasks, it isn't actually designed for them—its very name is stream editor.
The tool that is designed for non-interactive file editing is ex
. (vi
is the "visual editor" form of ex
.) Particularly when you want to edit files in place, ex
is a far superior tool to sed
.
In this case the commands used are almost identical to the sed
command given earlier, but they don't have to be. The following is POSIX compliant (unlike sed -i
).
for file in *.php; do ex -sc '%s/[ăâ]/a/ge | %s/ș/s/ge | %s/ț/t/ge | %s/î/i/ge | x' "$file" ; done
Explanation:
-s
starts "silent mode" in preparation for batch processing. -c
specifies the command(s) to be executed.
%
means to apply the following command to every line in the file. The s///
commands are fairly self-explanatory; the e
flag at the end means that any errors (due to the pattern not being found) are suppressed and file processing will continue.
|
is a command separator (not a pipe).
x
tells ex
to write any changes to the file (but only if there were changes) and exit.
If you want in-place file editing, ex
is the tool of choice. If you want to preview the changes before you make them, I'd recommend using tr
as @gardenhead suggests.
(Of course, if you're using a proper version control system such as git, you could make the changes in place using ex
and compare the files to the old version by running git diff
.)
echo ă â î ș ț | iconv -t us//TRANSLIT
which outputsa ^a ^i s t
on OS/X (anda a i s t
on GNU). – Stéphane Chazelas Jan 19 '16 at 12:19