假设像这样的ini风格的文件,
[Group]
Icon=xxx.ico
Title=An Image Editor
Description=Manipulates .ico, .png and .jpeg images
Run Code Online (Sandbox Code Playgroud)
我想在以(或匹配)"Icon ="开头的行中替换/删除".ico"
我在尝试这个:
oldline="`cat "$file" | grep "Icon="`"
newline="`echo "$oldline" | tr ".ico" ".png"`"
cat "$oldfile" | tr "$oldline" "$newline" > $file
Run Code Online (Sandbox Code Playgroud)
然后我意识到这tr与我想的完全不同.它不是一个传统的"替换为此"功能.所以我想正确的方法是使用sed.但:
sed过.不知道它是如何工作的.这有点矫枉过正吗?sed,鉴于它是如此强大,有没有任何优雅的方法来实现这一点,而不是这个"获取行 - >修改行 - >替换oldline for newline in file"方法?笔记:
提前致谢!
编辑:
感谢你们!这是最终的脚本,作为参考:
#! /bin/bash
# Fix the following WARNING in ~/.xsession-errors
# gnome-session[2035]: EggSMClient-WARNING: Desktop file '/home/xxx/.config/autostart/skype.desktop' has malformed Icon key 'skype.png'(should not include extension)
file="$HOME/.config/autostart/skype.desktop"
if [ -f "$file" ] ; then
if `cat "$file" | grep "Icon=" | grep -q ".png"` ; then
sed -i.bak '/^Icon=/s/\.png$//' "$file"
cp "$file" "$PWD"
cp "${file}.bak" "$PWD"
else
echo "Nothing to fix! (maybe fixed already?)"
fi
else
echo "Skype not installed (yet...)"
fi
Run Code Online (Sandbox Code Playgroud)
比我原来的更光滑!我唯一遗憾的是sed备份不保留原始文件时间戳.但我可以忍受这一点.
而且,为了记录,是的,我已经创建了这个脚本来修复Skype包装中的实际"错误".
像sed中的以下内容应该做你需要的.首先,我们检查线是否以Icon=,如果是,那么我们运行s命令(即替换).
sed -i '/^Icon=/s/\.ico$/.png/' file
Run Code Online (Sandbox Code Playgroud)
编辑:上面的sed脚本也可以这样写:
/^Icon=/ { # Only run the following block when this matches
s/\.ico$/.png/ # Substitute '.ico' at the end of the line with '.png'
}
Run Code Online (Sandbox Code Playgroud)
有关如何限制命令运行的详细信息,请参阅此页面.
| 归档时间: |
|
| 查看次数: |
16315 次 |
| 最近记录: |