The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to change only the x first characters of a string? Juha Shell Programming and Scripting 2 09-28-2007 05:17 AM
Removing characters from a string mh53j_fe Shell Programming and Scripting 3 06-03-2005 09:35 AM
Need help to extract a string delimited by any special character kumariak Shell Programming and Scripting 24 06-03-2005 06:20 AM
replacing string with special character ??? imppayel Shell Programming and Scripting 4 12-08-2004 01:07 AM
removing characters from end of string el_toro Shell Programming and Scripting 4 07-14-2002 05:02 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
Add string after another string with special characters

Hello everyone,

I'm writing a script to add a string to an XML file, right after a specified string that only occurs once in the file. For testing purposes I created a file 'testfile' that looks like this:
Code:
1
2
3
4
5
6
6
7
8
9
And this is the script as far as I've managed:
Code:
XMLSTRING="<thema xmlns=\"x-schema:df.ase\" uid=\"asd_F\" displayname=\"Jdddf 1\" asd_jg_id=\"6\" asd_jg_id_an=\"F\"/>"

awk '{ if ( $0 ~ /4/ ) {
        printf( "%s\n%s\n", $0, "'"$XMLSTRING"'" );
     }else{
        print $0;
     }
}' testfile
In this case, what I want it to to is to find '4' (will be a string in an XML file later) and to insert the value of $XMLSTRING on the line underneath. This all works fine when $XMLSTRING has a value without any strange characters, but in this case I get an error:
Code:
awk: cmd. line:2: 	 	printf( "%s\n%s\n", $0, "<thema xmlns=\"x-schema:df.ase\" uid=\"asd_F\" displayname=\"Jdddf 1\" asd_jg_id=\"6\" asd_jg_id_an=\"F\"/>" );
awk: cmd. line:2: 	 	                                                ^ syntax error
In case the formatting mangles this, the syntax error points to the first ':' in the inserted string.

My question is; in what way can I handle this so that it does print the string correctly? What I'm looking for in this case, is this:
Code:
1
2
3
4
<thema xmlns="x-schema:df.ase" uid="asd_F" displayname="Jdddf 1" asd_jg_id="6" asd_jg_id_an="F"/>
5
6
7
7
8
9
Any help would be greatly appreciated!
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-21-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,665
Place the xml string within single quotes and use an awk variable.
This should work:

Code:
#!/bin/sh

XMLSTRING='<thema xmlns=\"x-schema:df.ase\" uid=\"asd_F\" displayname=\"Jdddf 1\" asd_jg_id=\"6\" asd_jg_id_an=\"F\"/>'

awk -v var="$XMLSTRING" '
  {if ( $0 ~ /4/ ) {
    printf( "%s\n%s\n", $0, var )
  }
  else {
    print $0
  }
}' testfile
Regards
Reply With Quote
  #3 (permalink)  
Old 03-21-2008
Registered User
 

Join Date: Mar 2008
Posts: 2
Quote:
Originally Posted by Franklin52 View Post
Place the xml string within single quotes and use an awk variable.
This should work:

Code:
#!/bin/sh

XMLSTRING='<thema xmlns=\"x-schema:df.ase\" uid=\"asd_F\" displayname=\"Jdddf 1\" asd_jg_id=\"6\" asd_jg_id_an=\"F\"/>'

awk -v var="$XMLSTRING" '
  {if ( $0 ~ /4/ ) {
    printf( "%s\n%s\n", $0, var )
  }
  else {
    print $0
  }
}' testfile
Regards
Wow, that worked, thanks! I didn't know you could pass variables to awk like that, this is great!

Thanks again!
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0