简体   繁体   English

Bash - 如何删除等号周围的任何空格

[英]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: 这种方法的最大优点是:

  • it will strip down every "cosmetic" things (extra spaces, etc). 它将剥离每一个“化妆品”的东西(额外的空间等)。 Ie it will not only replace name = value with name=value but will get rid of many other extra spaces, and indent "neatly" and tersely. 也就是说,它不仅将取代name = valuename=value ,但将摆脱许多其他多余的空格,和缩进“整齐”和简洁。
  • it knows XML, and therefore is much safer to use than any thing based on regular expressions (you CAN'T parse every xml files with regular expressions! It will work for basic xml structures, but is far from encomassing every thing an XML file can contain) 它知道XML,因此使用比基于正则表达式的任何东西都安全得多(你不能用正则表达式解析每个xml文件!它将适用于基本的xml结构,但远远不能包含XML文件的所有东西)包含)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM