[英]Bash - How to remove any empty spaces around an equal sign
I have an xml file which I'm trying to minimise using a bash script. 我有一个xml文件,我试图使用bash脚本最小化。
After removing any indentation, some part of the file looks like this: 删除任何缩进后,文件的某些部分如下所示:
<layer name ="dotted_line"
align ="topleft"
edge ="topleft"
handcursor ="false"
keep ="true"
url ="%SWFPATH%/include/info_btn/dotted_line.png"
zorder ="15"
/>
I'd like to remove any empty spaces before and after the equal sign, so it'd look like: 我想在等号之前和之后删除任何空格,所以它看起来像:
<layer name="dotted_line"
align="topleft"
edge="topleft"
handcursor="false"
keep="true"
url="%SWFPATH%/include/info_btn/dotted_line.png"
zorder="15"
/>
Any ideas how can achieve this? 任何想法怎么能实现这个?
Thanks 谢谢
sed -is/\\ *=\\ */=/g filename
问候
这应该工作
sed 's/\s*=\s*/=/g' inputFile
use tr
? 使用tr
?
$ echo 'hello there' | tr -d "[:space:]"
hellothere
$
Of course this'll trash ALL spaces in the file, which may be a bit too much. 当然这会丢弃文件中的所有空格,这可能有点太多了。
You should first try to see if xmllint --format isn't already doing everything you need: 您应该首先尝试查看xmllint --format是否还没有完成您需要的所有操作:
xmllint --format OLDFILE.xml > NEWFILE.xml
The big advantage of this approach: 这种方法的最大优点是:
name = value
with name=value
but will get rid of many other extra spaces, and indent "neatly" and tersely. 也就是说,它不仅将取代name = value
与name=value
,但将摆脱许多其他多余的空格,和缩进“整齐”和简洁。 another way, if you are SURE that the name ="value" are each alone on their own line: 换句话说,如果你确定name =“value”各自独立:
export _tab_="$(printf '\011')"
sed -e "s/^[ ${_tab_}]*[a-zA-Z0-9_-]\([a-zA-Z0-9_-]*\)[ ${_tab_}][ ${_tab_}]*=[ ${_tab_}][ ${_tab_}]*/\1=/" OLDFILE.xml > NEWFILE.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.