Evan Pal McDan
Evan Pal McDan

Reputation: 53

Need Sed or any script to remove whitespace between characters and replace with underscores

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 whitespaces between CC and = and replace with underscores like this:

CC_MY_APPS=My aplications
CC_APPLICATION_SETTINGS_TITLE=My Settings

Upvotes: 1

Views: 391

Answers (3)

pavel
pavel

Reputation: 3508

Alternatively, if your values don't contain a '=', you could try:

perl -pi.bak -e 's/\s+(?=.*=)/_/g' file.ini

using a look-ahead.

Upvotes: 0

Toto
Toto

Reputation: 91518

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

Upvotes: 0

kurumi
kurumi

Reputation: 25609

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

Upvotes: 2

Related Questions