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.
agama
December 28, 2011, 1:48pm
2
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
birei
December 28, 2011, 4:28pm
3
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 :
agama:
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
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
agama
December 28, 2011, 11:04pm
4
birei:
@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
birei
December 29, 2011, 3:50am
5
@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 . 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.
birei
December 29, 2011, 10:24am
7
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.
agama
December 29, 2011, 11:32am
9
shuklaa02:
Thanks a lot Birei and Agama, this is great help . 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
.
Good grief -- I made that change without thinking (and obviously without testing). Good catch and nice fix!!