Need help in getting count from xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in getting count from xml file
# 1  
Old 10-18-2017
Need help in getting count from xml file

Team,
We need help in getting the customer count of multiple xml file present in a directory.

My xml structure is like below :
Code:
  
 <?xml version="1.0" encoding="ISO-8859-15"?>
<customers xmlns="Demandware - Commerce Platform | eCommerce Software">
<customer customer-no="2253998836943556"><credentials><login>susan@nealfam.com</login><password encrypted="true" encryptionScheme="scrypt">$s0$b0401$jzFfnnT2Z8SZiiJ+hT1nfA==$GdFi0r5ATbTjzRc2KdqYpxyQBqBJVgCl/E0qYdqHRws=</password><enabled-flag>true</enabled-flag><password-question></password-question><password-answer></password-answer></credentials><profile><first-name>Susan</first-name><second-name></second-name><last-name>Neal</last-name><email>susan@nealfam.com</email><phone-home></phone-home><phone-business></phone-business><phone-mobile></phone-mobile><fax></fax><creation-date>2000-04-20T02:18:36.000Z</creation-date><preferred-locale></preferred-locale><custom-attributes><custom-attribute  attribute-id="legacyTransition">notcomplete</custom-attribute><custom-attribute  attribute-id="legacyAccount">true</custom-attribute><custom-attribute  attribute-id="rewardsMemberFlag">false</custom-attribute><custom-attribute  attribute-id="rewardsID"></custom-attribute><custom-attribute  attribute-id="rewardsEmail">susan@nealfam.com</custom-attribute><custom-attribute  attribute-id="promoPref">false</custom-attribute><custom-attribute  attribute-id="rewardsPref">false</custom-attribute><custom-attribute  attribute-id="legacyID">2756</custom-attribute><custom-attribute  attribute-id="hasBrandedCard">false</custom-attribute><custom-attribute  attribute-id="hasPaypal"></custom-attribute></custom-attributes></profile><addresses><address address-id="56297194367043129" preferred="false"><first-name>Susan</first-name><second-name></second-name><last-name>Neal</last-name><address1>Gymboree</address1><address2>500 Howard St</address2><postbox></postbox><city>San Francisco</city><postal-code>94105</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>4152787561</phone><custom-attributes><custom-attribute  attribute-id="addressType">BIL</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194380317767" preferred="true"><first-name>Susan</first-name><second-name></second-name><last-name>Neal</last-name><address1>2745 Lake St</address1><address2></address2><postbox></postbox><city>San Francisco</city><postal-code>94121-1047</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>4157501501</phone><custom-attributes><custom-attribute  attribute-id="addressType">BIL,SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194365826730" preferred="false"><first-name>Susan</first-name><second-name></second-name><last-name>Neal</last-name><address1>2745 Lake St</address1><address2></address2><postbox></postbox><city>San Francisco</city><postal-code>94121</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>650-696-7561</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194365845003" preferred="false"><first-name>Zachary</first-name><second-name></second-name><last-name>Neal</last-name><address1>19 Milldam Road</address1><address2></address2><postbox></postbox><city>Acton</city><postal-code>01720</postal-code><state-code>MA</state-code><country-code>US</country-code><phone>9782632093</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194365899569" preferred="false"><first-name>Christian  Oliver and Xander</first-name><second-name></second-name><last-name>Picot</last-name><address1>19304 Overleaf Lane</address1><address2></address2><postbox></postbox><city>Davidson</city><postal-code>28036</postal-code><state-code>NC</state-code><country-code>US</country-code><phone>631-283-7027</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194365923826" preferred="false"><first-name>Mr. and Mrs. Seth</first-name><second-name></second-name><last-name>Bain</last-name><address1>Small Pond Studios</address1><address2>254 Ritch St</address2><postbox></postbox><city>San Francisco</city><postal-code>94107</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>415-498-2105</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194366021537" preferred="false"><first-name>Carter</first-name><second-name></second-name><last-name>Croke</last-name><address1>5692 South Nome Street</address1><address2></address2><postbox></postbox><city>Englewood</city><postal-code>80111</postal-code><state-code>CO</state-code><country-code>US</country-code><phone>650-696-7561</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194366053342" preferred="false"><first-name>Mr. and Mrs. Ward</first-name><second-name></second-name><last-name>Supplee</last-name><address1>301 22nd Avenue</address1><address2></address2><postbox></postbox><city>San Mateo</city><postal-code>94403</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>650-799-0032</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194366053343" preferred="false"><first-name>Beth</first-name><second-name></second-name><last-name>sususus</last-name><address1>123 Lakdfj</address1><address2></address2><postbox></postbox><city>San Francisco</city><postal-code>94121</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>650-696-7561</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194366142219" preferred="false"><first-name>sdddds</first-name><second-name></second-name><last-name>ssss</last-name><address1>dsdsds</address1><address2></address2><postbox></postbox><city>SAN FRANCISCO</city><postal-code>94121</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>650-696-8888</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address></addresses></customer>
<customer customer-no="2253998836943557"><credentials><login>susan1@nealfam.com</login><password encrypted="true" encryptionScheme="scrypt">$s0$b0401$jzFfnnT2Z8SZiiJ+hT1nfA==$GdFi0r5ATbTjzRc2KdqYpxyQBqBJVgCl/E0qYdqHRws=</password><enabled-flag>true</enabled-flag><password-question></password-question><password-answer></password-answer></credentials><profile><first-name>Susan</first-name><second-name></second-name><last-name>Neal</last-name><email>susan@nealfam.com</email><phone-home></phone-home><phone-business></phone-business><phone-mobile></phone-mobile><fax></fax><creation-date>2000-04-20T02:18:36.000Z</creation-date><preferred-locale></preferred-locale><custom-attributes><custom-attribute  attribute-id="legacyTransition">notcomplete</custom-attribute><custom-attribute  attribute-id="legacyAccount">true</custom-attribute><custom-attribute  attribute-id="rewardsMemberFlag">false</custom-attribute><custom-attribute  attribute-id="rewardsID"></custom-attribute><custom-attribute  attribute-id="rewardsEmail">susan@nealfam.com</custom-attribute><custom-attribute  attribute-id="promoPref">false</custom-attribute><custom-attribute  attribute-id="rewardsPref">false</custom-attribute><custom-attribute  attribute-id="legacyID">2756</custom-attribute><custom-attribute  attribute-id="hasBrandedCard">false</custom-attribute><custom-attribute  attribute-id="hasPaypal"></custom-attribute></custom-attributes></profile><addresses><address address-id="56297194367043129" preferred="false"><first-name>Susan</first-name><second-name></second-name><last-name>Neal</last-name><address1>Gymboree</address1><address2>500 Howard St</address2><postbox></postbox><city>San Francisco</city><postal-code>94105</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>4152787561</phone><custom-attributes><custom-attribute  attribute-id="addressType">BIL</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194380317767" preferred="true"><first-name>Susan</first-name><second-name></second-name><last-name>Neal</last-name><address1>2745 Lake St</address1><address2></address2><postbox></postbox><city>San Francisco</city><postal-code>94121-1047</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>4157501501</phone><custom-attributes><custom-attribute  attribute-id="addressType">BIL,SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194365826730" preferred="false"><first-name>Susan</first-name><second-name></second-name><last-name>Neal</last-name><address1>2745 Lake St</address1><address2></address2><postbox></postbox><city>San Francisco</city><postal-code>94121</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>650-696-7561</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194365845003" preferred="false"><first-name>Zachary</first-name><second-name></second-name><last-name>Neal</last-name><address1>19 Milldam Road</address1><address2></address2><postbox></postbox><city>Acton</city><postal-code>01720</postal-code><state-code>MA</state-code><country-code>US</country-code><phone>9782632093</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194365899569" preferred="false"><first-name>Christian  Oliver and Xander</first-name><second-name></second-name><last-name>Picot</last-name><address1>19304 Overleaf Lane</address1><address2></address2><postbox></postbox><city>Davidson</city><postal-code>28036</postal-code><state-code>NC</state-code><country-code>US</country-code><phone>631-283-7027</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194365923826" preferred="false"><first-name>Mr. and Mrs. Seth</first-name><second-name></second-name><last-name>Bain</last-name><address1>Small Pond Studios</address1><address2>254 Ritch St</address2><postbox></postbox><city>San Francisco</city><postal-code>94107</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>415-498-2105</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194366021537" preferred="false"><first-name>Carter</first-name><second-name></second-name><last-name>Croke</last-name><address1>5692 South Nome Street</address1><address2></address2><postbox></postbox><city>Englewood</city><postal-code>80111</postal-code><state-code>CO</state-code><country-code>US</country-code><phone>650-696-7561</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194366053342" preferred="false"><first-name>Mr. and Mrs. Ward</first-name><second-name></second-name><last-name>Supplee</last-name><address1>301 22nd Avenue</address1><address2></address2><postbox></postbox><city>San Mateo</city><postal-code>94403</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>650-799-0032</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194366053343" preferred="false"><first-name>Beth</first-name><second-name></second-name><last-name>sususus</last-name><address1>123 Lakdfj</address1><address2></address2><postbox></postbox><city>San Francisco</city><postal-code>94121</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>650-696-7561</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address><address address-id="56297194366142219" preferred="false"><first-name>sdddds</first-name><second-name></second-name><last-name>ssss</last-name><address1>dsdsds</address1><address2></address2><postbox></postbox><city>SAN FRANCISCO</city><postal-code>94121</postal-code><state-code>CA</state-code><country-code>US</country-code><phone>650-696-8888</phone><custom-attributes><custom-attribute  attribute-id="addressType">SHP</custom-attribute><custom-attribute  attribute-id="isLegacy">true</custom-attribute></custom-attributes></address></addresses></customer>
</customers>


From the above sample xml ,we need to take the total count of customer no from the file. So we need to consider the total no of 'customer-no' present in that file.

In a directory ,many similar xml files will be present.so we need to provide the total ''customer-no' present in all the files.

Can anyone kindly help me to getting the count of total customer from the all the files in a directory through unix scripting. As I'm new to scripting ,any help here will be appreciated.
# 2  
Old 10-18-2017
Try
Code:
grep -hc customer-no *.xml

# 3  
Old 10-18-2017
Hi Rudy/All,
By using the below command ,we are getting individual file count like below.
Code:
  
  grep -hc customer-no *.xml
31220
56492
57483
59503
64170
67882
68599
69292
66854
69652
70398
34722

We need total distinct count from the all the xml files available in the directory. Kindly help me in this regard.
# 4  
Old 10-18-2017
So why the "distinct" count, all of a sudden? You didn't mention that before. How do you tell customers from each other?

As for the sum, try
Code:
echo $(( $(grep -hc customer-no *.xml | tr '\n' '+' ) 0 ))

This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-18-2017
Hi Rudy,
Thanx a lot for your help.
I understand the 1st part

Code:
 grep -hc customer-no *.xml

Could you kindly explain me how sum is calculating here for my understanding purpose.
# 6  
Old 10-18-2017
grep gives you this
Code:
grep -hc customer-no *.xml
31220
56492
57483
59503
64170
67882
68599
69292
66854
69652
70398
34722

Which is the representation of this
Code:
31220\n56492\n57483\n59503\n64170\n67882\n68599\n69292\n66854\n69652\n70398\n34722\n

The command tr '\n' '+' translates any new line for a plus sign
Code:
31220+56492+57483+59503+64170+67882+68599+69292+66854+69652+70398+34722+

Display the sum of it
Code:
echo $((31220+56492+57483+59503+64170+67882+68599+69292+66854+69652+70398+34722+0))

# 7  
Old 10-18-2017
You have a list of integer numbers in successive lines; tr converts <NL> (\n, 0x0A) characters to plus signs so you have 31220+56492+...+34722+. The trailing + is considered offensive by the now to be applied "arithmetic evaluation" $(( ... )), so a solitary zero is appended.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pull multiple XML tags from the same XML file in Shell.?

I'm searching for the names of a TV show in the XML file I've attached at the end of this post. What I'm trying to do now is pull out/list the data from each of the <SeriesName> tags throughout the document. Currently, I'm only able to get data the first instance of that XML field using the... (9 Replies)
Discussion started by: hungryd
9 Replies

2. UNIX for Beginners Questions & Answers

Grepping multiple XML tag results from XML file.

I want to write a one line script that outputs the result of multiple xml tags from a XML file. For example I have a XML file which has below XML tags in the file: <EMAIL>***</EMAIL> <CUSTOMER_ID>****</CUSTOMER_ID> <BRANDID>***</BRANDID> Now I want to grep the values of all these specified... (1 Reply)
Discussion started by: shubh752
1 Replies

3. Shell Programming and Scripting

Splitting a single xml file into multiple xml files

Hi, I'm having a xml file with multiple xml header. so i want to split the file into multiple files. Sample.xml consists multiple headers so how can we split these multiple headers into multiple files in unix. eg : <?xml version="1.0" encoding="UTF-8"?> <ml:individual... (3 Replies)
Discussion started by: Narendra921631
3 Replies

4. Shell Programming and Scripting

Split xml file into multiple xml based on letterID

Hi All, We need to split a large xml into multiple valid xml with same header(2lines) and footer(last line) for N number of letterId. In the example below we have first 2 lines as header and last line as footer.(They need to be in each split xml file) Header: <?xml version="1.0"... (5 Replies)
Discussion started by: vx04
5 Replies

5. Shell Programming and Scripting

Splitting xml file into several xml files using perl

Hi Everyone, I'm new here and I was checking this old post: /shell-programming-and-scripting/180669-splitting-file-into-several-smaller-files-using-perl.html (cannot paste link because of lack of points) I need to do something like this but understand very little of perl. I also check... (4 Replies)
Discussion started by: mcosta
4 Replies

6. Shell Programming and Scripting

Comparing delta values of one xml file in other xml file

Hi All, I have two xml files. One is having below input <NameValuePair> <name>Daemon</name> <value>tcp:7474</value> </NameValuePair> <NameValuePair> <name>Network</name> <value></value> </NameValuePair> ... (2 Replies)
Discussion started by: sharsour
2 Replies

7. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

8. Shell Programming and Scripting

Help required in Splitting a xml file into multiple and appending it in another .xml file

HI All, I have to split a xml file into multiple xml files and append it in another .xml file. for example below is a sample xml and using shell script i have to split it into three xml files and append all the three xmls in a .xml file. Can some one help plz. eg: <?xml version="1.0"?>... (4 Replies)
Discussion started by: ganesan kulasek
4 Replies

9. Shell Programming and Scripting

How to add the multiple lines of xml tags before a particular xml tag in a file

Hi All, I'm stuck with adding multiple lines(irrespective of line number) to a file before a particular xml tag. Please help me. <A>testing_Location</A> <value>LA</value> <zone>US</zone> <B>Region</B> <value>Russia</value> <zone>Washington</zone> <C>Country</C>... (0 Replies)
Discussion started by: mjavalkar
0 Replies

10. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies
Login or Register to Ask a Question