I have an large language .ini file whose lines look like this:
CC MY APPS=My aplications
CC MY APPLICATION SETTINGS TITLE=My Settings
I need to remove all whitespace开发者_StackOverflows between CC and = and replace with underscores like this:
CC_MY_APPS=My aplications
CC_APPLICATION_SETTINGS_TITLE=My Settings
It is better to use awk
$ awk 'BEGIN{OFS=FS="="}{gsub(/ /,"_",$1)}1' file
CC_MY_APPS=My aplications
CC_MY_APPLICATION_SETTINGS_TITLE=My Settings
Alternatively if you have Ruby(1.9+)
$ ruby -F"=" -ane '$F[0].gsub!(/\s+/,"_");puts $F.join("=")' file
A perl way to do it, very similar to kurumi's solution in Ruby :
perl -i.orig -F= -ane '$F[0]=~s/\s+/_/g;print join"=",@F' file.ini
The original file will be saved into file.ini.orig
Alternatively, if your values don't contain a '=', you could try:
perl -pi.bak -e 's/\s+(?=.*=)/_/g' file.ini
using a look-ahead.
精彩评论