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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:11:23+00:00 2026-05-11T18:11:23+00:00

I have a XML file with the following data format: <net NetName=abc attr1=123 attr2=234

  • 0

I have a XML file with the following data format:

<net NetName="abc" attr1="123" attr2="234" attr3="345".../>
<net NetName="cde" attr1="456" attr2="567" attr3="678".../>
....

Can anyone tell me how could I data mine the XML file using an awk one-liner? For example, I would like to know attr3 of abc. It will return 345 to me.

  • 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-11T18:11:23+00:00Added an answer on May 11, 2026 at 6:11 pm

    In general, you don’t. XML/HTML parsing is hard enough without trying to do it concisely, and while you may be able to hack together a solution that succeeds with a limited subset of XML, eventually it will break.

    Besides, there are many great languages with great XML parsers already written, so why not use one of them and make your life easier?

    I don’t know whether or not there’s an XML parser built for awk, but I’m afraid that if you want to parse XML with awk you’re going to get a lot of “hammers are for nails, screwdrivers are for screws” answers. I’m sure it can be done, but it’s probably going to be easier for you to write something quick in Perl that uses XML::Simple (my personal favorite) or some other XML parsing module.

    Just for completeness, I’d like to note that if your snippet is an example of the entire file, it is not valid XML. Valid XML should have start and end tags, like so:

    <netlist>
      <net NetName="abc" attr1="123" attr2="234" attr3="345".../>
      <net NetName="cde" attr1="456" attr2="567" attr3="678".../>
      ....
    </netlist>
    

    I’m sure invalid XML has its uses, but some XML parsers may whine about it, so unless you’re dead set on using an awk one-liner to try to half-ass “parse” your “XML,” you may want to consider making your XML valid.

    In response to your edits, I still won’t do it as a one-liner, but here’s a Perl script that you can use:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use XML::Simple;
    
    sub usage {
      die "Usage: $0 [NetName] ([attr])\n";
    }
    
    my $file = XMLin("file.xml", KeyAttr => { net => 'NetName' });
    
    usage() if @ARGV == 0;
    
    exists $file->{net}{$ARGV[0]}
      or die "$ARGV[0] does not exist.\n";
    
    
    if(@ARGV == 2) {
      exists $file->{net}{$ARGV[0]}{$ARGV[1]}
        or die "NetName $ARGV[0] does not have attribute $ARGV[1].\n";
      print "$file->{net}{$ARGV[0]}{$ARGV[1]}.\n";
    
    } elsif(@ARGV == 1) {
      print "$ARGV[0]:\n";
      print "  $_ = $file->{net}{$ARGV[0]}{$_}\n"
        for keys %{ $file->{net}{$ARGV[0]} };
    
    } else {
      usage();
    }
    

    Run this script from the command line with 1 or 2 arguments. The first argument is the 'NetName' you want to look up, and the second is the attribute you want to look up. If no attribute is given, it should just list all the attributes for that 'NetName'.

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

Sidebar

Related Questions

I have a xml file main.xml with following markup and data. main.xml <xml> <content>
I have the following xml file: <xfa:data> <form1> <Page1> <Page2> <contractInfo> ... </contractInfo> <paymentInfo>
I have an xml file in the following format: <food> <desert> cake <desert> </food>
I have a XML File in the following format: <?xml version='1.0' encoding='UTF-8'?> <entry xmlns='http://www.w3.org/2005/Atom'
I have an xml document with the following format: <response username=123 customerName=CustomerName siteName=SiteName customerID=123
I have the following XML file containing letter and word definitions in this format
I have XML file in the following format; <root> <entry> <details> </entry> <entry> <details>
I have following data in XML response in JMeter: <details> <srNo>1</srNo> <key>123</key> <Name>Inspector</piName> <age>89</age>
I have following xml file: <ab> <![CDATA[ <table> <tbody> <tr> <th>abcdef</th> </tr> <tr> <p>
I have an xml file with the following structure: <main_tag> <first> <tag1>val1</tag1> <conf> <tag2>val2</tag2>

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.