Problem with awk while handling special charaters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with awk while handling special charaters
# 1  
Old 07-25-2008
Problem with awk while handling special charaters

Hi,

I have an application.xml file like
</dependency>
<artifactId>_AdminServicesEAR</artifactId>
<version>1.0.0-20080521.085352-1</version>
<context-root>oldvalue</context-root>
<type>ear</type>
<DOCTYPE "abc/xyz/eft">
<NewTag>value123</xyz>
<NewTag>value321</abcd>

</dependency>


i need to search & replace the value inside <NewTag> based on the input parameter which iam passing to the script.

eg : ./sampleawk 1 will replace the first occurance of <NewTag>
The command which i am using is

awk -v n=$OCCURENCE -v s="<$KEY>$VALUE<\/" "/<$KEY>(.+)<\//&&n==++c{sub(\"<$KEY>(.+)<\/\",s)}1"


But this command is not working properly if the "$VALUE" contains some
special characters like & , % etc...

CAn anyone help me to change the exisiting command for handling special charcters ???

Thanks in advance
# 2  
Old 07-25-2008
You are already escaping the slash; escaping the values in $VALUE before passing it in would seem like the simplest change. Maybe write a wrapper script for passing in OCCURRENCE (correctly spelled :^), KEY, and VALUE properly escaped would be the way to go.
# 3  
Old 07-25-2008
Code:
$n=shift;
$new=shift;
open (FH,"<file");
while(<FH>){
	if(m/<NewTag>/){
		$t++;
	}
	if($t==$n){
		s/<NewTag>/<$new>/;
	}
	print;
}

Code:
perl sample.pl 1 perl

# 4  
Old 07-25-2008
You could use sed like this to insert appropriate escapes:

Code:
OCCURRENCE=2
VALUE='test&test$test'
NEWVALUE=$(echo "$VALUE" | sed 's/&/\\\\&/g;s/[$]/\\\$/g')
KEY=NewTag
awk -v n=$OCCURRENCE -v s="<$KEY>$NEWVALUE<\/" "/<$KEY>(.+)<\//&&n==++c{sub(\"<$KEY>(.+)<\/\",s)}1"

ETA: cross-posted, looks like I simply implemented era's suggestion.
# 5  
Old 07-25-2008
Thanks a lot all ...

Its working fine for me........
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

problem handling lov in FOR loop

Hi all, i am trying to process a list of values in FILE like this: aaa:bbb ccc:ddd eee:fff With the following logic: for INFO in FILE do export F1=`cut -f1,3,5,7 -d":" < FILE` export F2=`cut -f2,3,5,7 -d":" < FILE` ssh $F1 bash <<EOF echo $F1 echo $F2 date (10 Replies)
Discussion started by: jonnyd
10 Replies

2. Shell Programming and Scripting

script to tail file; problem with awk and special characters

Trying to use code that I found to send only new lines out of a log file by doing: while :; do temp=$(tail -1 logfile.out) awk "/$last/{p=1}p" logfile.out #pipe this to log analyzer program last="$temp" sleep 10 done Script works fine when logfile is basic text, but when it contains... (2 Replies)
Discussion started by: moo72moo
2 Replies

3. Infrastructure Monitoring

Perl Error Handling Problem

I can get this working, but if something is down I get an error and the script does not move on. I can not get the "else" function working. What might I be doing wrong? use SNMP::Simple my %ios = (); $list="list.list"; open(DAT, $list) || die("Can't Open List"); @raw_data=<DAT>;... (4 Replies)
Discussion started by: mrlayance
4 Replies

4. UNIX for Advanced & Expert Users

replace word with special charaters

I have input file called file1 with characters that have \\ in it. I cannot change input file, because it is generated earlier in script. Now would like to replace string on line in file called bfile with output from file1 I have been using sed command. $cat file1 pc//6sPxp== $ cat scr1... (4 Replies)
Discussion started by: drtabc
4 Replies

5. Programming

problem in reforking and signal handling

hi friends i have a problem in signal handling ... let me explain my problem clearly.. i have four process .. main process forks two child process and each child process again forks another new process respectively... the problem is whenever i kill the child process it is reforking and the... (2 Replies)
Discussion started by: senvenugopal
2 Replies

6. Shell Programming and Scripting

special characters handling in perl

Hi, Here is my piece of code-- sub per_user_qna_detail { for($index=0;$index<@records;$index++) { if($records =~ m/^(.*)\s*Morocco.*Entering\s*Module::authenticate/) { printf "INSIDE per_user_qna_detail on LINE NO $index\n"; $Time_Stamp = $1;... (0 Replies)
Discussion started by: namishtiwari
0 Replies

7. Programming

Problem with handling SIGINT

For a program I am designing, which involves handling the keyboard input Ctrl^c (SIGINT), it is taking ages for the program to actually recognise and perform the corresponding action whenever I run it and hit Ctrl^C at the CL. I have to do at least 3 Ctrl^Cs before the program will actually... (3 Replies)
Discussion started by: JamesGoh
3 Replies

8. Shell Programming and Scripting

Handling special characters using awk

Hi all, How do I extract a value without special characters? I need to extract the value of %Used from below and if its greater than 80, need to send a notification. I am doing this right now..Its giving 17%..Is there a way to extract the value and assign it to a variable in one step? df |grep... (3 Replies)
Discussion started by: sam_78_nyc
3 Replies

9. UNIX for Advanced & Expert Users

handling special characters

Hello everyone, I use Samba to copy mp3 files to my Red Hat 8.0 box so I can randomize them through a playlist. When I copy: Sigur Rós-Nýja Lagið.mp3 It shows in the mapped drive on Windows as: Sigur Rós-N_ja Lagi_.mp3 And via Putty as: Sigur R(grayed box)s-N_ja Lagi_.mp3 What is going... (1 Reply)
Discussion started by: effigy
1 Replies

10. Solaris

Handling Special Charcters

Dear All, I have created a UTF-8 database to store multi-lingual charcters. Below is the query from which i insert from Winsql (front-end third party database browser tool), the data gets inserted properly. insert into a (no, lbl) values (1, "Cliquez ici pour revenir Ã_ la recherche de... (2 Replies)
Discussion started by: lloydnwo
2 Replies
Login or Register to Ask a Question