Replacing tab value in xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing tab value in xml file
# 8  
Old 01-12-2015
Hi RudiC

Great. I can understand the code. I have got in problem if I make little change in the entries -

If I shift values to middle in the line which means it contains heading spaces then repacement does not happen -
Code:
                               keystoremyPass="xxx" />
                               myword="xxxxx"
<Environment name="TTY_DAPP_OSL_KEYSTORE_MYWORD" value="xxx" type="java.lang.String"/>
<Environment name="TTY_SNO_SPNEGO_MYWORD" value="xxxx" type="java.lang.String"/>
<Environment name="IIT_DAPP_BIND_MYWORD" value="xxxxxx" type="java.lang.String"/>

output would be --
Code:
                                       keystoremyPass="xxx" />
                                       myword="xxxx"
<Environment name="TTY_DAPP_OSL_KEYSTORE_MYWORD" value="---" type="java.lang.String"/>
<Environment name="TTY_SNO_SPNEGO_MYWORD" value="---" type="java.lang.String"/>
<Environment name="IIT_DAPP_BIND_MYWORD" value="---" type="java.lang.String"/>

This means
Code:
$1

field contains additional beginning spaces along with string. Please suggest how to deal with this.

Thanks
# 9  
Old 01-12-2015
Hello krsnadasa,

Following may help you, could you please check and let me know.
Code:
awk     '{gsub(/^ +/,X,$0);n = split (KW, TMP, ",")
                         for (; n>0; n--) PAT[TMP[n]]
                        }
         $1 in PAT      {$2="---"}
         $2 in PAT      {$4="---"}
         1
        ' FS="\"" OFS="\"" KW="keystoremyPass=,myword=,TTY_DAPP_OSL_KEYSTORE_MYWORD,TTY_SNO_SPNEGO_MYWORD,IIT_DAPP_BIND_MYWORD" Input_file

Output will be as follows.
Code:
keystoremyPass="---" />
myword="---"
<Environment name="TTY_DAPP_OSL_KEYSTORE_MYWORD" value="---" type="java.lang.String"/>
<Environment name="TTY_SNO_SPNEGO_MYWORD" value="---" type="java.lang.String"/>
<Environment name="IIT_DAPP_BIND_MYWORD" value="---" type="java.lang.String"/>

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 10  
Old 01-13-2015
Hi Ravinder

Thanks for reply. However I do not want to delete the leading spaces which means I do not want records to be shifting to left end either. Still I should be able to match the record.

I tried using split function -

I tried replacing below line :
Code:
$1 in PAT      {$2="---"}

to
Code:
split($1,myarr," "); myarr[2] in PAT $2="---"}

But seems i am breaking the code altogether. Any idea?

Thanks
# 11  
Old 01-13-2015
Try
Code:
awk     'NR==1          {n = split (KW, TMP, ",")
                         for (; n>0; n--) PAT[TMP[n]]
                        }
                        {T=$1; sub (/^ */, "", T)}
         T  in PAT      {$2="---"}
         $2 in PAT      {$4="---"}
         1
        ' FS="\"" OFS="\"" KW="keystoremyPass=,myword=,TTY_DAPP_OSL_KEYSTORE_MYWORD,TTY_SNO_SPNEGO_MYWORD,IIT_DAPP_BIND_MYWORD" file
                               keystoremyPass="---" />
                               myword="---"
<Environment name="TTY_DAPP_OSL_KEYSTORE_MYWORD" value="---" type="java.lang.String"/>
<Environment name="TTY_SNO_SPNEGO_MYWORD" value="---" type="java.lang.String"/>
<Environment name="IIT_DAPP_BIND_MYWORD" value="---" type="java.lang.String"/>

---------- Post updated at 13:28 ---------- Previous update was at 13:22 ----------

Regarding your own proposal: Did you consider peeking into myarr? What be the contents of myarr[2]?
Try myarr[1] in PAT ...
This User Gave Thanks to RudiC For This Post:
# 12  
Old 01-15-2015
Hi RudiC

This works great the way you defined. But when I do little different as you have advised me in earlier post it does not work -
What I did is kept all search patterns in another file -

mypattern.txt -
Code:
TTY_DAPP_OSL_KEYSTORE_MYWORD
TTY_SNO_SPNEGO_MYWORD
IIT_DAPP_BIND_MYWORD
keystoremyPass
myword

Code:
nawk 'BEGIN{FS="\"" ;OFS="\""} NR==FNR{Err[$1]++;next}
{split($1,myarr," ")}
myarr[1] in Err {$2="---"}; $2 in Err { $4="---"}}1' mypattern.txt  file

OutPut -
Code:
                               keystoremyPass="xxx" />
                               myword="xxx"
<Environment name="TTY_DAPP_OSL_KEYSTORE_MYWORD" value="---" type="java.lang.String"/>
<Environment name="TTY_SNO_SPNEGO_MYWORD" value="---" type="java.lang.String"/>
<Environment name="IIT_DAPP_BIND_MYWORD" value="---" type="java.lang.String"/>

This proves that array
Code:
myarr[1]

have all the patterns stored there.
even if I use
Code:
sub

function in place of
Code:
split

as you did still same output comes. Any idea?

Thanks

---------- Post updated 01-15-15 at 07:32 AM ---------- Previous update was 01-14-15 at 07:57 AM ----------

Hello RudiC

My bad. I was doing minor mistake. In mypattern file I am missing
Code:
=

for
Code:
 keystoremyPass,myword

.
It should be like that -
Code:
TTY_DAPP_OSL_KEYSTORE_MYWORD
TTY_SNO_SPNEGO_MYWORD
IIT_DAPP_BIND_MYWORD
keystoremyPass=
myword=

Really Thank you and Ravi for all along help. Great learning thanks guys!!

Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert tab separated text to non-trivial xml. (pwsafe --> KeePassx)

I'd like to take the output of `pwsafe --exportdb > database.txt` and convert it to a KeePassX XML friendly format (feature request in pwsafe). I found flat file converter but the syntax is beyond me with this example. Solutions are welcomed. More details Here is the pwsafe --> KeePassX XML... (2 Replies)
Discussion started by: graysky
2 Replies

2. Shell Programming and Scripting

Replacing a value in all xml's

Hi Folks, Could you please advise what will be the unix command to replace the character in all xml's under a particular directory for example let say I rite now at the following below location $ cd /opt/apr/rt/conf now under conf there are so many xml's and in those xml's i want to... (2 Replies)
Discussion started by: punpun66
2 Replies

3. Shell Programming and Scripting

Replacing a file in between a XML file

I am having XML file,i need to replace a range of string and replace with a file,Pls anyone help, My Xml file looks like </Order><PersonInfoShipTo AddressID="446311709" AddressLine1="" AddressLine2="" AddressLine3="" AddressLine4="" AddressLine5="" AddressLine6="" AlternateEmailID="" Beeper=""... (2 Replies)
Discussion started by: mohanalakshmi
2 Replies

4. Shell Programming and Scripting

Reading XML and replacing a string

Hi All, My thanks in advance for you guys reading this and for any posts. I'm having 100 XML files, I need script which replace a specific string. It must be incrementing in 100 xml files.. Sample XML files: <hwIPHostName type="attrib">DEMO1</hwIPHostName> I need Demo1 to be... (4 Replies)
Discussion started by: karty2129
4 Replies

5. Shell Programming and Scripting

Replacing part of XML code inside comment tags

Hello! I'd like to modify custom values in a XML config file between comment tags using bash script. <feature> <keyboardshortcut>C-m</keyboardshortcut> <option1>disabled</option2> <option2>enabled</option2> </feature> <!-- bash script features START --> <feature> ... (2 Replies)
Discussion started by: prism1
2 Replies

6. Shell Programming and Scripting

Replacing number between xml tags with ksh shell script

Hallo, im basically a complete noob on shell scripting and im trying to replace or rather add 1 to a number between xml tags. The xml basically has a tag somewhere that looks like this: <tag>12345678901234</tag> Now i want to replace the number between the tags. And i want the file to... (6 Replies)
Discussion started by: Demoric
6 Replies

7. Shell Programming and Scripting

Replacing the last record in xml with different tags

I have special requirement, my system provided the xml file as below(available xml file) and I need to convert it as below desired xml file. is it possible thru shell scripts or awk? What I need is : my available xml contains number of records with tags <RevenueAmounts>, the last of record is... (6 Replies)
Discussion started by: LinuxLearner
6 Replies

8. Shell Programming and Scripting

Read and copy xml line by line and preserve tab?

I'm trying to read an xml file and copy it line by line to another file and want to preserve the tabs. What i'm trying to do is if I get to a certain line in the xml, I'm going to check to see if the next line is specifically what I want. If it's not, then I want to insert a single line of text... (4 Replies)
Discussion started by: DeuceLee
4 Replies

9. UNIX for Dummies Questions & Answers

Replacing Comma by Tab

Hi All, i have a file test.txt as shown below, 1,test,test111 2,rest,rest222 i want to replace the commas by tab delimiter.., it should be like, 1 test test111 2 rest rest222 i tried the following code, sed 's/,/\\t/g' test.txt >> ouptut.txt (9 Replies)
Discussion started by: Serious Sam
9 Replies

10. Shell Programming and Scripting

replacing string in an XML file

Hi all, I need to replace string in XML file..XML file like <dependency> <groupId>fr.orange.portail.ear</groupId> <artifactId>_AdminServicesEAR</artifactId> <version>1.0.0-20080521.085352-1</version> <type>ear</type> </dependency> <dependency> ... (2 Replies)
Discussion started by: subin_bala
2 Replies
Login or Register to Ask a Question