为何sed无法替换行末字符串?技术求助
Solution to Sed Substitution Failure
Root Cause
Your sed command targets .decode('utf-8') (with a hyphen), but the line you want to modify contains .decode('utf8') (without a hyphen). The pattern doesn’t match the actual text, so no changes are applied—this is why you see no error but no effect.
Corrected Command
Use the exact string from the line in your sed substitution:
sed -i "3328s/.decode('utf8')//" /usr/local/lib/python3.6/site-packages/keras/engine/topology.py
Verify the Change
Run this command to confirm the line is modified:
sed -n '3328p' /usr/local/lib/python3.6/site-packages/keras/engine/topology.py
You should see the updated line:
original_keras_version = f.attrs['keras_version']
Optional: Robust Pattern for Trailing Whitespace
If the line has trailing spaces after .decode('utf8'), adjust the pattern to handle them:
sed -i "3328s/.decode('utf8')\s*//" /usr/local/lib/python3.6/site-packages/keras/engine/topology.py
The \s* matches zero or more whitespace characters, ensuring the substitution works regardless of trailing spaces.
内容的提问来源于stack exchange,提问作者Manolo Dominguez Becerra
相关产品推荐
相关产品推荐

