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 1037353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:50:33+00:00 2026-05-16T14:50:33+00:00

I want to add an attribute to every tag in my xml, which is

  • 0

I want to add an attribute to every tag in my xml, which is incrementing using either awk, sed, perl or plain shell cmd

For Eg:

<tag1 key="123">
  <tag2 abc="xf d"/>
  <tag3 def="d2 32">
   </tag3>
</tag1>

I am expecting the following output

<tag1 key="123" order="1">
  <tag2 abc="xf d" order="2"/>
  <tag3 def="d2 32" order="3">
   </tag3>
</tag1>

If possible I am not looking on any dependencies(Twig,LibXML), pure string manipulation.

  • 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-16T14:50:34+00:00Added an answer on May 16, 2026 at 2:50 pm

    Normally you should use a proper parser to process xml. But in awk:

    awk 'match($0, /<[^\/>]+/) { \
         $0 = substr($0, 1, RSTART+RLENGTH-1) " order=\"" ++i "\"" \
              substr($0, RSTART+RLENGTH) \
         }; 1'
    

    I look for a opening tag (without the > or /> part) on every the line. If found, put the string order="i" after it, while incrementing i. The single 1 on the last line just always executes awk‘s default action: { print $0 }.

    I updated the regular expression to work on your revised input. It fails as soon as you have multiple opening tags on a single line, etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to add the disabled attribute on the form:hidden tag dynamically when i
I had to add an href attribute to the following xml but not every
I'm doing a simple slideUp animation on an object. I want to add attribute
I want to add custom attribute settings to a generated anchor link of Wordpress.
In my E-commerce application, I want to add a attribute to a Product, only
I want to add a rel attribute of the element matching the array, so
I want add label to every row in tableview at right side of the
Hey, I want to process one attribute of every object inside one queryset, and
I want to add the data-val-required and data-val attributes to an @html.textbox or an
I want to add class atttributes to a superclass dynamically. Furthermore, I want to

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.