![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 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
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 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 |
|
||||
|
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
|
|
||||
|
Quote:
Thanks again! |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|