Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6973569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:07:30+00:00 2026-05-27T17:07:30+00:00

I have to write a script that will count the number of xml tags(say

  • 0

I have to write a script that will count the number of xml tags(say Code) in a xml file using shell script. XML file can be anyone of the following formats:

Format #1: 
<Code>value1</Code> <Code>value2</Code>

 Format #2: 
<Code Attr1=va>value1</Code> <Code Attr1=va
Attr2=va>value1</Code>

Format #3: 
<Code>value1</Code><Code>value2</Code> (All Codes can be in
a single line or multiple lines)

Format #4 
   <Code Attr1=va>value1</Code><Code Attr2=va>value1</Code>

Format #5: 
<Cod 
e>Value1</Code
<Code Attr=1> </C
ode>

In short XML file can in any format and can have new lines anywhere.
Please help me, I need to do this soon..

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:07:31+00:00Added an answer on May 27, 2026 at 5:07 pm

    Regular expressions are a bad way to parse XML, using some sort of XML parser is better.

    If you really want to use sed/awk/shell/grep etc, the first thing I can think of is:

     cat tst | xargs | grep -o '<\s*C\s*o\s*d\s*e[^>]*>' | wc -l
    

    I don’t know awk very well, but I’m sure there are awk ninjas out there who can do it more elegantly than this.

    It only counts occurences of <Code> (& variations) but not the closing tag, so if you have (for example) 10 <Code> in your file but only 9 </Code>, it will return 10 and not 9.

    Basically:

    • cat tst | xargs cats ‘tst’ to the shell all on one line (so I don’t have to worry about new lines);
    • grep -o '<\s*C\s*o\s*d\s*e[^>]*>' prints all matches of <Code{optional other stuff}> where you can have newlines/spaces in between all letters of Code (the -o prints just the matches to the regex, one per line);
    • wc -l counts the lines.

    Try each bit successively to see what I mean.

    For me tst was just a copy-paste of what you have above.

    [foo@bar ~]$cat tst
    Format #1: 
    <Code>value1</Code> <Code>value2</Code>
    
     Format #2: 
    <Code Attr1=va>value1</Code> <Code Attr1=va
    Attr2=va>value1</Code>
    
    Format #3: 
    <Code>value1</Code><Code>value2</Code> (All Codes can be in
    a single line or multiple lines)
    
    Format #4 
       <Code Attr1=va>value1</Code><Code Attr2=va>value1</Code>
    
    Format #5: 
    <Cod 
    e>Value1</Code
    <Code Attr=1> </C
    ode>
    
    [foo@bar ~]$cat tst | xargs
    Format #1: <Code>value1</Code> <Code>value2</Code> Format #2: <Code Attr1=va>value1</Code> <Code Attr1=va Attr2=va>value1</Code> Format #3: <Code>value1</Code><Code>value2</Code> (All Codes can be in a single line or multiple lines) Format #4 <Code Attr1=va>value1</Code><Code Attr2=va>value1</Code> Format #5: <Cod e>Value1</Code <Code Attr=1> </C ode>
    
    [foo@bar ~]$cat tst | xargs | grep -o '<\s*C\s*o\s*d\s*e[^>]*>'
    <Code>
    <Code>
    <Code Attr1=va>
    <Code Attr1=va Attr2=va>
    <Code>
    <Code>
    <Code Attr1=va>
    <Code Attr2=va>
    <Cod e>
    <Code Attr=1>
    
    [foo@bar ~]$cat tst | xargs | grep -o '<\s*C\s*o\s*d\s*e[^>]*>' | wc -l
    10
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an upload script that write a index.html file, I modified it to
i need to write a vba script that will find a file. the file
I have been tasked to write a script that will enable tcp and named
I'm looking to write a script that will load up a file that has
I am trying to write a script that will read a remote sitemap.xml and
I have to write an Nant script that will accept 2 parameters on the
Background I have been asked to write a script that will create a vertical
How can I write a script that will change an input field's text value,
I have to write a Matlab script that does this: The input is 2
I have to write a bash script that makes lot of things. I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.