The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 08:17 AM
Removing characters from a string mh53j_fe Shell Programming and Scripting 3 06-03-2005 12:35 PM
Need help to extract a string delimited by any special character kumariak Shell Programming and Scripting 24 06-03-2005 09:20 AM
replacing string with special character ??? imppayel Shell Programming and Scripting 4 12-08-2004 05:07 AM
removing characters from end of string el_toro Shell Programming and Scripting 4 07-14-2002 08:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-21-2008
heliode heliode is offline
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!
  #2 (permalink)  
Old 03-21-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
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
  #3 (permalink)  
Old 03-21-2008
heliode heliode is offline
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!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0