我正在尝试替换Mac上Makefile中的字符串,以便交叉编译为iOS。字符串中嵌入了双引号。该命令为:
sed -i "" 's|"iphoneos-cross","llvm-gcc:-O3|"iphoneos-cross","clang:-Os|g' Configure错误是:
sed: RE error: illegal byte sequence我试着转义双引号、逗号、破折号和冒号,但没有任何乐趣。例如:
sed -i "" 's|\"iphoneos-cross\"\,\"llvm-gcc\:\-O3|\"iphoneos-cross\"\,\"clang\:\-Os|g' Configure我在调试这个问题时遇到了很大的困难。有人知道如何让sed打印非法字节序列的位置吗?或者有人知道非法的字节序列是什么?
发布于 2013-11-04 22:54:06
将以下行添加到~/.bash_profile或~/.zshrc文件中。
export LC_CTYPE=C
export LANG=C发布于 2018-02-19 23:52:57
我的变通方法是使用Perl:
find . -type f -print0 | xargs -0 perl -pi -e 's/was/now/g'发布于 2016-01-28 03:22:22
mklement0's answer很棒,但我有一些小调整。
在使用iconv. 时明确指定 bash 的编码似乎是个好主意。 此外,我们应该在前面加上一个字节顺序标记(even though the unicode standard doesn't recommend it),因为there can be legitimate confusions between UTF-8 and ASCII without a byte-order mark。 不幸的是,iconv 在显式指定字节序(UTF-16BE 或UTF-16LE)时不会预先添加字节顺序标记,因此我们需要使用UTF-16,它使用特定于平台的字节序,然后使用file --mime-encoding以发现使用的真正字节顺序iconv。
(我所有的编码都是大写的,因为当你用iconv -l列出所有iconv支持的编码时,它们都是大写的。)
# Find out MY_FILE's encoding
# We'll convert back to this at the end
FILE_ENCODING="$( file --brief --mime-encoding MY_FILE )"
# Find out bash's encoding, with which we should encode
# MY_FILE so sed doesn't fail with
# sed: RE error: illegal byte sequence
BASH_ENCODING="$( locale charmap | tr [:lower:] [:upper:] )"
# Convert to UTF-16 (unknown endianness) so iconv ensures
# we have a byte-order mark
iconv -f "$FILE_ENCODING" -t UTF-16 MY_FILE > MY_FILE.utf16_encoding
# Whether we're using UTF-16BE or UTF-16LE
UTF16_ENCODING="$( file --brief --mime-encoding MY_FILE.utf16_encoding )"
# Now we can use MY_FILE.bash_encoding with sed
iconv -f "$UTF16_ENCODING" -t "$BASH_ENCODING" MY_FILE.utf16_encoding > MY_FILE.bash_encoding
# sed!
sed 's/.*/&/' MY_FILE.bash_encoding > MY_FILE_SEDDED.bash_encoding
# now convert MY_FILE_SEDDED.bash_encoding back to its original encoding
iconv -f "$BASH_ENCODING" -t "$FILE_ENCODING" MY_FILE_SEDDED.bash_encoding > MY_FILE_SEDDED
# Now MY_FILE_SEDDED has been processed by sed, and is in the same encoding as MY_FILEhttps://stackoverflow.com/questions/19242275
复制相似问题