Inline edit using sed / awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inline edit using sed / awk
# 1  
Old 12-28-2011
Inline edit using sed / awk

Hi,
I have file with all the lines as following format
Code:
<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
Code:
<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.

Last edited by jim mcnamara; 12-28-2011 at 02:28 PM.. Reason: please use code tags to format your examples
# 2  
Old 12-28-2011
This will probably do the trick. It will handle a file that has lines with different formats if needed.

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

# 3  
Old 12-28-2011
Hi shuklaa02,

A perl one-line:
Code:
$ 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:

Quote:
Originally Posted by agama
This will probably do the trick. It will handle a file that has lines with different formats if needed.

Code:
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:
Code:
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
This User Gave Thanks to birei For This Post:
# 4  
Old 12-29-2011
Quote:
Originally Posted by birei
@agama:



I think you forgot to lowercase the string. I modified it to:
Code:
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:

Code:
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

# 5  
Old 12-29-2011
@agama:

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

Regards,
Birei
# 6  
Old 12-29-2011
Inline edit using sed / awk

Thanks a lot Birei and Agama, this is great help Smilie. BTW the output is slightly different from expected, following is printing value of 'name' in lower case while truncating rest
Code:
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)
Code:
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.
This User Gave Thanks to shuklaa02 For This Post:
# 7  
Old 12-29-2011
The 'perl' script fixed:
Code:
$ perl -lape '
BEGIN { sub modify { $str = $_[0]; $str =~ tr|/|.|; lc $str } } 
s/(name=")[^"]*("\s+nameInNameSpace=")([^"]*")/$1 . modify($3) . $2 . $3/ge
' infile

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed, Inline replacement of string with spaces

Hello, Just surfed on the web for probable answers but could not get them working. I wish to replace the string containing spaces by another phrase but below answers did not work. My string is: PAIN & GAIN I wish to convert it to: P&G I just need it working with sed with function -i ... (6 Replies)
Discussion started by: baris35
6 Replies

2. UNIX for Advanced & Expert Users

Combining awk Inline and File Code

I've ended up with a small collection of libraries I like to use with awk, but this means I can't do awk -f librarycode.awk '{ program code }' filename because awk will assume that anything after -f is a filename, not code. Is there any way I can do both? (6 Replies)
Discussion started by: Corona688
6 Replies

3. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

4. Shell Programming and Scripting

edit field using sed or awk

please help me to edit the second field using awk or sed i have input file below aa1001 000001 bb1002 000002 cc1003 000003 so i want the output file like below aa1001 01 bb1002 02 cc1003 03 (38 Replies)
Discussion started by: zulabc
38 Replies

5. Shell Programming and Scripting

Awk or Sed, fubd match in column, then edit column.

FILE A: 9780743551526,(Abridged) 9780743551779,(Unabridged) 9780743582469,(Abridged) 9780743582483,(Unabridged) 9780743563468,(Abridged) 9780743563475,(Unabridged) FILE B: c3saCandyland 9780743518321 "CANDYLAND" "MCBAIN, ED" 2001 c3sbCandyland 9780743518321 ... (7 Replies)
Discussion started by: glev2005
7 Replies

6. UNIX for Advanced & Expert Users

awk - remove block of text, multiple actions for 'if', inline edit

I'm having a couple of issues. I'm trying to edit a nagios config and remove a host definition if a certain "host_name" is found. My thought is I would find host definition block containing the host_name I'm looking for and output the line numbers for the first and last lines. Using set, I will... (9 Replies)
Discussion started by: mglenney
9 Replies

7. Shell Programming and Scripting

Sed or Awk or both to edit file

What is an efficient way to remove all lines from the input file which contain a file name? inputfile: ======================= # comment # comment # comment 5 8 10 /tmp 5 8 10 /var/run 5 8 10 /etc/vfstab 5 8 9 /var/tmp 5 8 10 /var/adm/messages... (7 Replies)
Discussion started by: Arsenalman
7 Replies

8. Shell Programming and Scripting

File edit with awk or sed

I have the follwoing file: This looks to be : seperated. For the first field i want only the file name without ".txt" and also i want to remove "+" sign if the second field starts with "+" sign. Input file: Output file: Appreciate your help (9 Replies)
Discussion started by: pinnacle
9 Replies

9. Shell Programming and Scripting

edit entire column from a fixed-width file using awk or sed

Col1 Col2 Col3 Col4 12 Completed 08 0830 12 In Progress 09 0829 11 For F U 07 0828 Considering the file above, how could i replace the third column the most efficient way? The actual file size is almost 1G. I am... (10 Replies)
Discussion started by: tamahomekarasu
10 Replies

10. Shell Programming and Scripting

sed / awk - inplace or inline edit

I need to remove the '&' from a file. In each line of the file, the fields are separated by ^K. I only want to remove '&' if it exists in field number 9. (example of field 9: abc&xyz) I need to do an in place/in line edit. So far I have accomplished the following: awk -F '^K' '{print... (6 Replies)
Discussion started by: hemangjani
6 Replies
Login or Register to Ask a Question