You have multiple problems in your code. Don't use ls
in scripts and quote your variables. You should probably use $(command substitution)
rather than the legacy `command substitution`
syntax.
If your task is to replace ý
in the file's contents -- not in its name -- sed -i
is not wrong, but superfluous; just write the updated contents to the new location and delete the old file.
#!/bin/sh
for i in *.txt
do
filename=$(echo "$i" | cut -d'.' -f1)
sed 's/ý/|/g' "$i" >"${filename}.mnt" && rm "$i"
done
If your system is configured for UTF-8, the character ý
is representable with either the byte sequence
\xc3 \xbd
(representing U+00FD) or the decomposed sequence \0x79 \xcc \x81
(U+0079 + U+0301) - you might find that the file contains one representation, while your terminal prefers another. The only way to really be sure is to examine the hex bytes in the file and on your terminal. It is also entirely possible that your terminal is not capable of displaying the contents of the file exactly. Try
bash$ printf 'ý' | xxd
00000000: c3bd
bash$ head -c 16 file | xxd
00000000: 4245 4749 4e5f 5255 4e5f 5351 4cff 4445 BEGIN_RUN_SQL.DE
If (as here) you find that they are different (the latter outputs the single byte \xff
between "BEGIN_RUN_SQL" and "DE") then the trivial approach won't work. Your sed
may or may not support passing in literal hex sequences to say exactly what to substitute; or perhaps try e.g. Perl if not.