Add string after another string with special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add string after another string with special characters
# 1  
Old 03-21-2008
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  
Old 03-21-2008
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  
Old 03-21-2008
Quote:
Originally Posted by Franklin52
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to replace the string with special characters

{"name":"alR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","description":"aLR_pl-ENVIRONMENT_192_168_211_123_sDK_PROVISION_7","json_class":"Chef::Role","default_attributes":{},"override_attributes":{"yoapp":{"jboss":"5.1.0","port":"2243","warname":"soap","datacenter":"alR","ip":"192.168.211.123","... (3 Replies)
Discussion started by: nikhil jain
3 Replies

2. UNIX for Dummies Questions & Answers

Extract string between two special characters

Hi, I have a file that looks something like >1-18*anc... (12 Replies)
Discussion started by: jyu429
12 Replies

3. Shell Programming and Scripting

How to remove some special characters in a string?

Hi, I have string like this ="Lookup Procedure" But i want the output like this Lookup Procedure =," should be removed. Please suggest me the solution. Regards, Madhuri (2 Replies)
Discussion started by: srimadhuri
2 Replies

4. Shell Programming and Scripting

Extract string between 2 special characters

Hi, I have a unix file with contents as below Line1: ABC MNN X$$QWERTY$$ JKL Line2: HELLO $$HOW$$ ARE $$YOU$$ DOING i want to extract the string between $$ and $$ ie i want the output as QWERTY HOW YOU i want those strings seperated by some character say | desired output is... (7 Replies)
Discussion started by: vinredmac
7 Replies

5. Shell Programming and Scripting

Remove string between two special characters

Hi All, I have a variable like AVAIL="\ BACK:bkpstg:testdb3.iad.expertcity.com:backtest|\ #AUTH:authstg:testdb3.iad.expertcity.com:authiapd|\ TEST:authstg:testdb3.iad.expertcity.com:authiapd|\ " What I want to do here is that If a find # before any entry, remove the entire string... (5 Replies)
Discussion started by: engineermayur
5 Replies

6. Shell Programming and Scripting

Replacing string with special characters in shell

Hi, I am trying to replace a string in shell but it is not working correctly. @xcom.file@ needs to be replaced with tb137 Plz help.Thx. Please use and tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks. (4 Replies)
Discussion started by: manish72
4 Replies

7. Programming

C++ Special Characters in a String?

Hello. How can i put all of the special characters on my keyboard into a string in c++ ? I tried this but it doesn't work. string characters("~`!@#$%^&*()_-+=|\}]{ How can i accomplish this? Thanks in advance. (1 Reply)
Discussion started by: cbreiny
1 Replies

8. Shell Programming and Scripting

Remove special characters from string

Hi there, I'd like to write a script that removes any set of character from any string. The first argument would be the string, the second argument would be the characters to remove. For example: $ myscript "My name's Santiago. What's yours?" "atu" My nme's Snigo. Wh's yors? I wrote the... (11 Replies)
Discussion started by: chebarbudo
11 Replies

9. Shell Programming and Scripting

Want the substring of a string with special characters using perl

Hi, I am trying to separate the directory from the filename using the substring and it does not like it. $Complete_name="C:\ABCD\Reports_Split\090308.1630" I want $File_Name as 090308.1630 I tried using Substring function and it does not like slashes in the complete_name. Any... (5 Replies)
Discussion started by: sushma0907
5 Replies

10. UNIX for Dummies Questions & Answers

Help with find and replace w/string containing special characters

Can I get some help on this please, I have looked at the many post with similar questions and have tried the solutions and they are not working for my scenario which is: I have a text file (myfile) that contains b_log=$g_log/FILENAME.log echo "Begin processing file FILENAME " >> $b_log ... (4 Replies)
Discussion started by: CAGIRL
4 Replies
Login or Register to Ask a Question