Inline edit using sed / awk

Hi,
I have file with all the lines as following format

<namebindings:StringNameSpaceBinding xmi:id="StringNameSpaceBinding"  name="ENV_CONFIG_PATH"  nameInNameSpace="COMP/HOD/MYSTR/BACKOFFICE/ENV_CONFIG_PATH"  stringToBind="test"/>

I want to replace (all the lines) value of 'name' by value of 'nameInNameSpace' converted to lowercase with '/' substituted by '.'
For example, in this case, this line would become

<namebindings:StringNameSpaceBinding  xmi:id="StringNameSpaceBinding" name="comp.hod.mystr.backoffice.env_config_path"  nameInNameSpace="COMP/HOD/MYSTR/BACKOFFICE/ENV_CONFIG_PATH"  stringToBind="test"/>

Greatly appreciate any suggestion.
Many Thanks.

This will probably do the trick. It will handle a file that has lines with different formats if needed.

awk '
    /nameInNameSpace/ {
        match( $0, "nameInNameSpace=\"[^\"]*\"" )
        split( substr( $0, RSTART, RLENGTH ), a, "=" );
        gsub( "/", ".", a[2] );
        sub( "name=\"[^\"]*\"", "name=" a[2]  );
    }
    { print; }
' input-file >output-file

Hi shuklaa02,

A perl one-line:

$ cat infile
<namebindings:StringNameSpaceBinding xmi:id="StringNameSpaceBinding"  name="ENV_CONFIG_PATH"  nameInNameSpace="COMP/HOD/MYSTR/BACKOFFICE/ENV_CONFIG_PATH"  stringToBind="test"/>
$ perl -lape '
BEGIN { sub modify { $str = $_[0]; $str =~ tr|/|.|; lc $str } } 
s/(name=")[^"]*("\s+nameInNameSpace=")([^"]*)"/$1 . modify($3) . $2 . $3/ge
' infile
<namebindings:StringNameSpaceBinding xmi:id="StringNameSpaceBinding"  name="comp.hod.mystr.backoffice.env_config_path"  nameInNameSpace="COMP/HOD/MYSTR/BACKOFFICE/ENV_CONFIG_PATH  stringToBind="test"/>

Regards,
Birei

---------- Post updated at 22:28 ---------- Previous update was at 22:10 ----------

@agama:

I think you forgot to lowercase the string. I modified it to:

awk '
    /nameInNameSpace/ {
        match( $0, "nameInNameSpace=\"[^\"]*\"" )
        split( substr( $0, RSTART, RLENGTH ), a, "=" );
        gsub( "/", ".", a[2] );
        sub( "name=\"[^\"]*\"", "name=" a[2]  );
        $0 = tolower( $0 );
    }
    { print; }
' input-file >output-file

Regards,
Birei

Thanks. Actually I missed the lower case requirement, but I think just the replacement, not the whole line should be translated:

awk '
    /nameInNameSpace/ {
        match( $0, "nameInNameSpace=\"[^\"]*\"" )
        split( substr( $0, RSTART, RLENGTH ), a, "=" );
        gsub( "/", ".", a[2] );
        $0 = tolower( a[2] );
        sub( "name=\"[^\"]*\"", "name=" a[2]  );
    }
    { print; }
' input-file >output-file

@agama:

True, the whole line is incorrect. Your last code is the good one.

Regards,
Birei

Thanks a lot Birei and Agama, this is great help :b:. BTW the output is slightly different from expected, following is printing value of 'name' in lower case while truncating rest

awk '
    /nameInNameSpace/ {
        match( $0, "nameInNameSpace=\"[^\"]*\"" )
        split( substr( $0, RSTART, RLENGTH ), a, "=" );
        gsub( "/", ".", a[2] );
        $0 = tolower( a[2] );
        sub( "name=\"[^\"]*\"", "name=" a[2]  );
    }
    { print; }
' input-file >output-file

So have changed it slightly (with my basic awk and hit-n-trial)

awk '
    /nameInNameSpace/ {
        match( $0, "nameInNameSpace=\"[^\"]*\"" )
        split( substr( $0, RSTART, RLENGTH ), a, "=" );
        gsub( "/", ".", a[2] );
        sub( "name=\"[^\"]*\"", "name=" tolower ( a[2] )  );
    }
    { print; } 
' input-file >output-file 

The perl solution is fine, but missing trailing double quote "" in value of nameInNameSpace, I would be fine using awk script above but in case anyone looking for perl solution, would need some little amendment, any idea ?

Many Thanks.

The 'perl' script fixed:

$ perl -lape '
BEGIN { sub modify { $str = $_[0]; $str =~ tr|/|.|; lc $str } } 
s/(name=")[^"]*("\s+nameInNameSpace=")([^"]*")/$1 . modify($3) . $2 . $3/ge
' infile

Regards,
Birei

Thanks a lot Birei, this was helpful however was adding two trailing double quotes """" in the value of 'name', which I got rid of with minor amendment

perl -lape '
BEGIN { sub modify { $str = $_[0]; $str =~ tr|/|.|; lc $str } } 
s/(name=")[^"]*("\s+nameInNameSpace=")([^"]*[^"])/$1 . modify($3) . $2 . $3/ge
' infile

Thanks a lot for all the help guys.

Many Thanks.

Good grief -- I made that change without thinking (and obviously without testing). Good catch and nice fix!!