Replacing tab value in xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing tab value in xml file
# 1  
Old 12-29-2014
Replacing tab value in xml file

Hi

I am working on xml file. I have to make sure below lines containing values within quotes are replaced by some character like "-".
Below are different lines in xml file.

Code:
 
Pattern 1:
 
<Connector port=....
.....
keystoremyPass="xxx" />
 
Pattern 2:
 
myword="xxxxx"
 
Pattern 3:
 
<Environment name="TTY_DAPP_OSL_KEYSTORE_MYWORD" value="xxx" type="java.lang.String"/>
 
Pattern 4:
 
<Environment name="TTY_SNO_SPNEGO_MYWORD" value="xxxx" type="java.lang.String"/>
 
Pattern 5:
 
<Environment name="IIT_DAPP_BIND_MYWORD" value="xxxxxx" type="java.lang.String"/>

I tried sed - works only for first

Code:
sed '/myword=/s/ "\( [^"]*\) "/"---"/' test.xml
 
I tried AWK -
 
gawk 'BEGIN {FS="\""} /myword=/ {$2="..."; print $2}1' test.xml

I need code which have all these patterns.

Please advise.

Thanks

Last edited by Don Cragun; 12-29-2014 at 03:59 PM.. Reason: Change <code> and </code> to [code] and [/code].
# 2  
Old 12-29-2014
Not clear.

Try the g flag following the s command: s/.../.../g.
# 3  
Old 12-29-2014
how to make the font style change in echo while using mailx command. what i am using is below. i want the output in bold and tabular format. what is the way to do.
Code:
#!/bin/sh
df -H /usr/apps | grep -vE '^Filesystem'| awk '{ print $5}' | while read memory;
do
  echo $memory
    used=$(echo $memory| awk '{ print $1}' | cut -d'%' -f1  )
        if [ $used -ge 5 ]; then
echo -e  "Disk Usage Alert from Server  \n$(date) \nDisk space on /usr/apps \"($used%)\" on (Server) as on $(date)" |
                mail -s "Alert: Disk space exceeded on /usr/apps by $used%" pratima@abc.com
fi
done


Last edited by Franklin52; 01-14-2015 at 09:56 AM.. Reason: Please use code tags
# 4  
Old 12-30-2014
Hi RudiC
Thanks. What I am looking for is that. I need to read xml file and make sure whenever below lines encountered , i need follwing changes -
If I get line
Code:
keystoremyPass="xxx" />

I need to replace whatever value keystoremyPass variable contains to something junk .. so this should be replaced like -
Code:
keystoremyPass="---" />

Similarly once I get line -
Code:
myword="xxxxx"

It should be changed to
Code:
 myword="---"

Similarly if I get line
Code:
<Environment name="TTY_DAPP_OSL_KEYSTORE_MYWORD" value="xxx" type="java.lang.String"/>

It should be changed to
Code:
<Environment name="TTY_DAPP_OSL_KEYSTORE_MYWORD" value="---" type="java.lang.String"/>

Similarly for below lines as well -
Code:
<Environment name="TTY_SNO_SPNEGO_MYWORD" value="xxxx" type="java.lang.String"/>

to
Code:
<Environment name="TTY_SNO_SPNEGO_MYWORD" value="---" type="java.lang.String"/>

Code:
<Environment name="IIT_DAPP_BIND_MYWORD" value="xxxxxx" type="java.lang.String"/>

to
Code:
<Environment name="IIT_DAPP_BIND_MYWORD" value="---" type="java.lang.String"/>

Note: Line numbers are not known to me.

Hope this clears a bit. I will prefer awk but sed will also work. This will be fine to have multiple search conditions.

Thanks
# 5  
Old 12-30-2014
Hello krsnadasa,

Could you please try following and let us know if this helps. Let's say following is the input file.
Input file:
Code:
cat check_file1
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"/>

Following is the code.
Code:
awk '{sub(/\"[xX]+/,"\"---",$0);print}' check_file1

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"/>

NOTE: It will always look for 1 or more strings X or x in the "" double quotes.


Thanks,
R. Singh

Last edited by RavinderSingh13; 12-30-2014 at 02:11 AM..
# 6  
Old 12-30-2014
Hi Ravinder

Thanks for looking into this. I have made this working using sed -

Here is my solution -
Code:
sed -ie '/myword=/s#"\([^"]*\)"#"---"#;/keystorePass=/s#"\([^"]*\)"#"---"#;/_MYWORD/s#value="\([^"]*\)"#value="---"# ' myfile.xml

Basically what I am doing here is putting three different conditions into one sed statement to make sure I am on correct line, then trying to have regular expression which grabs the value between double quotes of tag value and make it as remembered pattern -
Code:
value="unknown value"

then replacing this with -
Code:
value="---"

The imp point here is that value between the double quotes are not known and may vary ...
Code:
value="unknown"

I am still keen if some one can do this in AWK.

Thanks
# 7  
Old 12-30-2014
How about this one, you can specify the search patterns as parameters to the script:
Code:
awk     'NR==1          {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" file
<Connector port=....
keystoremyPass="---" />
Pattern 2:
myword="---"
Pattern 3:
<Environment name="TTY_DAPP_OSL_KEYSTORE_MYWORD" value="---" type="java.lang.String"/>
Pattern 4:
<Environment name="TTY_SNO_SPNEGO_MYWORD" value="---" type="java.lang.String"/>
Pattern 5:
<Environment name="IIT_DAPP_BIND_MYWORD" value="---" type="java.lang.String"/>

If you supply the keywords in a file, a minor adaption would make that fly, too.
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