Extract an attribute value from XMLExtract specific value from block of dataRetrieve attribute value from xml with namespaces using xmllintExtract value from XMLHow to extract a XML from log fileextract xml tag value from the file to VariableChange value inside xml attribute using sed?How to extract selected value in XMLExtract select match value of same contents in XML file

Does "volatile" guarantee anything at all in portable C code for multi-core systems?

What's the use of bonding pad assignments?

Counter intuitive Bayesian theorem

Being flown out for an interview, is it ok to ask to stay a while longer to check out the area?

JPEG with transparent background

How to draw a double napped cone with TikZ

What is the difference between turbojet and turbofan engines?

Finding price of the power option

Suppress database change message in SQLCMD.exe

What would a life-form be like if it's antimatter-based

Will a nuclear country use nuclear weapons if attacked by conventional means by another nuclear country?

Weird spacing in aligned environment

Could the Ancient Egyptian hieroglyphs have been deciphered without the Rosetta Stone with modern tech?

How do I protect myself from bad contracting jobs?

Why do airports in the UK have so few runways?

Is it normal to use a clipper instead of an limiter?

What is the most efficient algorithm to compute polynomial coefficients from its roots?

Book including space travel - ships with spires or pillars

Is it unsafe to remove one stud from a load bearing wall?

Printing Command Line Unicode Chess Board

How would an unethical corporation go about extracting resources from a nation unwilling to co-operate?

What is the purpose behind a glass nozzle?

How to make this figure using pgfplots in Latex?

Why did Leia not want to tell Han about Luke being her twin brother?



Extract an attribute value from XML


Extract specific value from block of dataRetrieve attribute value from xml with namespaces using xmllintExtract value from XMLHow to extract a XML from log fileextract xml tag value from the file to VariableChange value inside xml attribute using sed?How to extract selected value in XMLExtract select match value of same contents in XML file






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









8

















Using Bash,



File:



<?xml version="1.0" encoding="UTF-8"?>
<blah>
<blah1 path="er" name="andy" remote="origin" branch="master" tag="true" />
<blah1 path="er/er1" name="Roger" remote="origin" branch="childbranch" tag="true" />
<blah1 path="er/er2" name="Steven" remote="origin" branch="master" tag="true" />

</blah>


I have tried the following:



grep -i 'name="andy" remote="origin" branch=".*"' <filename>


But it returns the whole line:



<blah1 path="er" name="andy" remote="origin" branch="master" tag="true" />


I would like to match the line based on the following:



name="andy"


I just want it to return:



master









share|improve this question























  • 3





    I guess I'll leave this here.

    – JoL
    Jul 12 at 16:07

















8

















Using Bash,



File:



<?xml version="1.0" encoding="UTF-8"?>
<blah>
<blah1 path="er" name="andy" remote="origin" branch="master" tag="true" />
<blah1 path="er/er1" name="Roger" remote="origin" branch="childbranch" tag="true" />
<blah1 path="er/er2" name="Steven" remote="origin" branch="master" tag="true" />

</blah>


I have tried the following:



grep -i 'name="andy" remote="origin" branch=".*"' <filename>


But it returns the whole line:



<blah1 path="er" name="andy" remote="origin" branch="master" tag="true" />


I would like to match the line based on the following:



name="andy"


I just want it to return:



master









share|improve this question























  • 3





    I guess I'll leave this here.

    – JoL
    Jul 12 at 16:07













8












8








8


2






Using Bash,



File:



<?xml version="1.0" encoding="UTF-8"?>
<blah>
<blah1 path="er" name="andy" remote="origin" branch="master" tag="true" />
<blah1 path="er/er1" name="Roger" remote="origin" branch="childbranch" tag="true" />
<blah1 path="er/er2" name="Steven" remote="origin" branch="master" tag="true" />

</blah>


I have tried the following:



grep -i 'name="andy" remote="origin" branch=".*"' <filename>


But it returns the whole line:



<blah1 path="er" name="andy" remote="origin" branch="master" tag="true" />


I would like to match the line based on the following:



name="andy"


I just want it to return:



master









share|improve this question

















Using Bash,



File:



<?xml version="1.0" encoding="UTF-8"?>
<blah>
<blah1 path="er" name="andy" remote="origin" branch="master" tag="true" />
<blah1 path="er/er1" name="Roger" remote="origin" branch="childbranch" tag="true" />
<blah1 path="er/er2" name="Steven" remote="origin" branch="master" tag="true" />

</blah>


I have tried the following:



grep -i 'name="andy" remote="origin" branch=".*"' <filename>


But it returns the whole line:



<blah1 path="er" name="andy" remote="origin" branch="master" tag="true" />


I would like to match the line based on the following:



name="andy"


I just want it to return:



master






grep xml






share|improve this question
















share|improve this question













share|improve this question




share|improve this question








edited Jul 11 at 20:54









glenn jackman

57.5k8 gold badges78 silver badges123 bronze badges




57.5k8 gold badges78 silver badges123 bronze badges










asked Jul 11 at 20:00









JohnJohn

433 bronze badges




433 bronze badges










  • 3





    I guess I'll leave this here.

    – JoL
    Jul 12 at 16:07












  • 3





    I guess I'll leave this here.

    – JoL
    Jul 12 at 16:07







3




3





I guess I'll leave this here.

– JoL
Jul 12 at 16:07





I guess I'll leave this here.

– JoL
Jul 12 at 16:07










5 Answers
5






active

oldest

votes


















38


















Use an XML parser for parsing XML data. With xmlstarlet it just becomes an XPath exercise:



$ branch=$(xmlstarlet sel -t -v '//blah1[@name="andy"]/@branch' file.xml)
$ echo $branch
master





share|improve this answer





















  • 10





    This is the better answer since it will continue to work even after someone decided to change the order of the attributes.

    – Hermann
    Jul 11 at 21:05







  • 4





    @Hermann Or changes the whitespace, or adds another element with attributes name="andy" branch="foo", or changes the character encoding, or puts an escaped " in the branch attribute, or or or... I agree; just use an XML parser!

    – marcelm
    Jul 12 at 8:16







  • 4





    branch=$(xmllint --xpath 'string(//blah1[@name="andy"]/@branch)' file.xml) is the equivalent command with xmllint.

    – David Conrad
    Jul 12 at 17:23






  • 3





    @DavidConrad make that an answer.

    – RonJohn
    Jul 13 at 0:05











  • @RonJohn Done. I also decided to change it to an absolute XPath.

    – David Conrad
    Jul 13 at 18:03


















8


















Use xmllint to extract the value of the attribute using XPath:



xmllint --xpath 'string(/blah/blah1[@name="andy"]/@branch)' file.xml


It's better to use an XML parser to process XML since the order of the attributes can change and line breaks could be inserted resulting in the name and branch attributes being in different lines of the file.






share|improve this answer

































    7


















    With grep:



    grep -Pio 'name="andy".*branch="K[^"]*' file



    • -P enable perl regular expressions (PCRE)


    • -i ignore case


    • -o print only matched parts

    In the regex, the K is a zero-width lookbehind to match the part before the K, but to not include it in the match.






    share|improve this answer


























    • Ah, using Grep, I tried to do this way, but I guess my knowledge was very limited and I kept getting frustrated :$

      – John
      Jul 11 at 20:21











    • Wonderful solution, I learn every day.

      – Edward
      Jul 11 at 20:28






    • 4





      Parsing XML using grep is asking for trouble. What if the order of the attributes changes? What if there's some other (non-blah1) element that has similar attributes? What if the branch name includes "? Also, why -i? XML element and attribute names are case-sensitive. Now, all of these things are bugs waiting to surface at some point in the future. I recommend using the proper tool for the job; an XML parser.

      – marcelm
      Jul 12 at 8:21











    • The -i is taken from OP and could be handy to handle the attribute values (Roger, Steven). If the branch name had an ", then it should have been escaped with &quot;. Yes, you're right, XML may change, have line breaks, etc. pp., an XML parser is definitely the better answer, but OP asked for grep and it could be that he knows what he is doing.

      – Freddy
      Jul 12 at 10:50


















    3


















    Using awk:



    awk '/name="andy"/ for (i=1;i<=NF;i++) if ($i ~ "branch=") sub(/branch=/, ""); gsub(/"/, ""); print $i ' input


    This will find a line containing name="andy" and then loop through each field in that line. If the field contains branch= we will remove branch= and all double quotes and print the remainder of the field.



    sub(/branch=/, "") is looking for a match of branch= and replacing it with "" (nothing)



    gsub is similar except it will replace globally (all occurances instead of just the first occurance).






    share|improve this answer




























    • Thank you so much, I will google to understand sub and gsub

      – John
      Jul 11 at 20:16











    • I wish I could rate this up but another answer is better as you mentioned.

      – John
      Jul 11 at 20:21











    • This is good but only works if branch is on the same line with name.

      – David Conrad
      Jul 12 at 16:28











    • @DavidConrad: Yes that is the requirement. If you notice, branch is on every line but OP only wants to return the value of branch that is on the same line as the name.

      – Jesse_b
      Jul 12 at 16:39











    • That isn't exactly the requirement, though, that's just the way this file happens to look. XML allows whitespace, so if you break the lines on spaces it will still work with the highest-upvoted answer but it will break with awk. It's a caveat people using this solution should be aware of. That said, this is a good quick-and-dirty solution, and I upvoted you.

      – David Conrad
      Jul 12 at 17:15


















    1


















    I think this works:



    $ grep -i 'name="andy" remote="origin" branch=".*"' <filename> | awk -F' ' 'print $5' | sed -E 's/branch="(.*)"/1/'
    master


    The awk part makes sure only branch="master" is returned, the sed part gives back what's between the double quotes with a reference (the 1 matches the part between the parentheses).



    Now I know there are a lot of people out here with far more knowledge on the art that is awk and sed, so I'm prepared for some criticism :-)






    share|improve this answer




























    • But I am passing in the file thought :$ Thanks a lot for the answer, I didn't think of using awk. I don't want to read each line, I kinda want to read the whole file and do this? Not possible?

      – John
      Jul 11 at 20:10












    • Editing my answer to show you how to pipe it through.

      – Edward
      Jul 11 at 20:11











    • This works, but like any solution that doesn't treat the XML as XML, it will stop working if the order of attributes changes or line breaks are inserted.

      – David Conrad
      Jul 13 at 18:05












    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f529670%2fextract-an-attribute-value-from-xml%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown


























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    38


















    Use an XML parser for parsing XML data. With xmlstarlet it just becomes an XPath exercise:



    $ branch=$(xmlstarlet sel -t -v '//blah1[@name="andy"]/@branch' file.xml)
    $ echo $branch
    master





    share|improve this answer





















    • 10





      This is the better answer since it will continue to work even after someone decided to change the order of the attributes.

      – Hermann
      Jul 11 at 21:05







    • 4





      @Hermann Or changes the whitespace, or adds another element with attributes name="andy" branch="foo", or changes the character encoding, or puts an escaped " in the branch attribute, or or or... I agree; just use an XML parser!

      – marcelm
      Jul 12 at 8:16







    • 4





      branch=$(xmllint --xpath 'string(//blah1[@name="andy"]/@branch)' file.xml) is the equivalent command with xmllint.

      – David Conrad
      Jul 12 at 17:23






    • 3





      @DavidConrad make that an answer.

      – RonJohn
      Jul 13 at 0:05











    • @RonJohn Done. I also decided to change it to an absolute XPath.

      – David Conrad
      Jul 13 at 18:03















    38


















    Use an XML parser for parsing XML data. With xmlstarlet it just becomes an XPath exercise:



    $ branch=$(xmlstarlet sel -t -v '//blah1[@name="andy"]/@branch' file.xml)
    $ echo $branch
    master





    share|improve this answer





















    • 10





      This is the better answer since it will continue to work even after someone decided to change the order of the attributes.

      – Hermann
      Jul 11 at 21:05







    • 4





      @Hermann Or changes the whitespace, or adds another element with attributes name="andy" branch="foo", or changes the character encoding, or puts an escaped " in the branch attribute, or or or... I agree; just use an XML parser!

      – marcelm
      Jul 12 at 8:16







    • 4





      branch=$(xmllint --xpath 'string(//blah1[@name="andy"]/@branch)' file.xml) is the equivalent command with xmllint.

      – David Conrad
      Jul 12 at 17:23






    • 3





      @DavidConrad make that an answer.

      – RonJohn
      Jul 13 at 0:05











    • @RonJohn Done. I also decided to change it to an absolute XPath.

      – David Conrad
      Jul 13 at 18:03













    38














    38










    38









    Use an XML parser for parsing XML data. With xmlstarlet it just becomes an XPath exercise:



    $ branch=$(xmlstarlet sel -t -v '//blah1[@name="andy"]/@branch' file.xml)
    $ echo $branch
    master





    share|improve this answer














    Use an XML parser for parsing XML data. With xmlstarlet it just becomes an XPath exercise:



    $ branch=$(xmlstarlet sel -t -v '//blah1[@name="andy"]/@branch' file.xml)
    $ echo $branch
    master






    share|improve this answer













    share|improve this answer




    share|improve this answer










    answered Jul 11 at 20:53









    glenn jackmanglenn jackman

    57.5k8 gold badges78 silver badges123 bronze badges




    57.5k8 gold badges78 silver badges123 bronze badges










    • 10





      This is the better answer since it will continue to work even after someone decided to change the order of the attributes.

      – Hermann
      Jul 11 at 21:05







    • 4





      @Hermann Or changes the whitespace, or adds another element with attributes name="andy" branch="foo", or changes the character encoding, or puts an escaped " in the branch attribute, or or or... I agree; just use an XML parser!

      – marcelm
      Jul 12 at 8:16







    • 4





      branch=$(xmllint --xpath 'string(//blah1[@name="andy"]/@branch)' file.xml) is the equivalent command with xmllint.

      – David Conrad
      Jul 12 at 17:23






    • 3





      @DavidConrad make that an answer.

      – RonJohn
      Jul 13 at 0:05











    • @RonJohn Done. I also decided to change it to an absolute XPath.

      – David Conrad
      Jul 13 at 18:03












    • 10





      This is the better answer since it will continue to work even after someone decided to change the order of the attributes.

      – Hermann
      Jul 11 at 21:05







    • 4





      @Hermann Or changes the whitespace, or adds another element with attributes name="andy" branch="foo", or changes the character encoding, or puts an escaped " in the branch attribute, or or or... I agree; just use an XML parser!

      – marcelm
      Jul 12 at 8:16







    • 4





      branch=$(xmllint --xpath 'string(//blah1[@name="andy"]/@branch)' file.xml) is the equivalent command with xmllint.

      – David Conrad
      Jul 12 at 17:23






    • 3





      @DavidConrad make that an answer.

      – RonJohn
      Jul 13 at 0:05











    • @RonJohn Done. I also decided to change it to an absolute XPath.

      – David Conrad
      Jul 13 at 18:03







    10




    10





    This is the better answer since it will continue to work even after someone decided to change the order of the attributes.

    – Hermann
    Jul 11 at 21:05






    This is the better answer since it will continue to work even after someone decided to change the order of the attributes.

    – Hermann
    Jul 11 at 21:05





    4




    4





    @Hermann Or changes the whitespace, or adds another element with attributes name="andy" branch="foo", or changes the character encoding, or puts an escaped " in the branch attribute, or or or... I agree; just use an XML parser!

    – marcelm
    Jul 12 at 8:16






    @Hermann Or changes the whitespace, or adds another element with attributes name="andy" branch="foo", or changes the character encoding, or puts an escaped " in the branch attribute, or or or... I agree; just use an XML parser!

    – marcelm
    Jul 12 at 8:16





    4




    4





    branch=$(xmllint --xpath 'string(//blah1[@name="andy"]/@branch)' file.xml) is the equivalent command with xmllint.

    – David Conrad
    Jul 12 at 17:23





    branch=$(xmllint --xpath 'string(//blah1[@name="andy"]/@branch)' file.xml) is the equivalent command with xmllint.

    – David Conrad
    Jul 12 at 17:23




    3




    3





    @DavidConrad make that an answer.

    – RonJohn
    Jul 13 at 0:05





    @DavidConrad make that an answer.

    – RonJohn
    Jul 13 at 0:05













    @RonJohn Done. I also decided to change it to an absolute XPath.

    – David Conrad
    Jul 13 at 18:03





    @RonJohn Done. I also decided to change it to an absolute XPath.

    – David Conrad
    Jul 13 at 18:03













    8


















    Use xmllint to extract the value of the attribute using XPath:



    xmllint --xpath 'string(/blah/blah1[@name="andy"]/@branch)' file.xml


    It's better to use an XML parser to process XML since the order of the attributes can change and line breaks could be inserted resulting in the name and branch attributes being in different lines of the file.






    share|improve this answer






























      8


















      Use xmllint to extract the value of the attribute using XPath:



      xmllint --xpath 'string(/blah/blah1[@name="andy"]/@branch)' file.xml


      It's better to use an XML parser to process XML since the order of the attributes can change and line breaks could be inserted resulting in the name and branch attributes being in different lines of the file.






      share|improve this answer




























        8














        8










        8









        Use xmllint to extract the value of the attribute using XPath:



        xmllint --xpath 'string(/blah/blah1[@name="andy"]/@branch)' file.xml


        It's better to use an XML parser to process XML since the order of the attributes can change and line breaks could be inserted resulting in the name and branch attributes being in different lines of the file.






        share|improve this answer














        Use xmllint to extract the value of the attribute using XPath:



        xmllint --xpath 'string(/blah/blah1[@name="andy"]/@branch)' file.xml


        It's better to use an XML parser to process XML since the order of the attributes can change and line breaks could be inserted resulting in the name and branch attributes being in different lines of the file.







        share|improve this answer













        share|improve this answer




        share|improve this answer










        answered Jul 13 at 18:00









        David ConradDavid Conrad

        2691 silver badge4 bronze badges




        2691 silver badge4 bronze badges
























            7


















            With grep:



            grep -Pio 'name="andy".*branch="K[^"]*' file



            • -P enable perl regular expressions (PCRE)


            • -i ignore case


            • -o print only matched parts

            In the regex, the K is a zero-width lookbehind to match the part before the K, but to not include it in the match.






            share|improve this answer


























            • Ah, using Grep, I tried to do this way, but I guess my knowledge was very limited and I kept getting frustrated :$

              – John
              Jul 11 at 20:21











            • Wonderful solution, I learn every day.

              – Edward
              Jul 11 at 20:28






            • 4





              Parsing XML using grep is asking for trouble. What if the order of the attributes changes? What if there's some other (non-blah1) element that has similar attributes? What if the branch name includes "? Also, why -i? XML element and attribute names are case-sensitive. Now, all of these things are bugs waiting to surface at some point in the future. I recommend using the proper tool for the job; an XML parser.

              – marcelm
              Jul 12 at 8:21











            • The -i is taken from OP and could be handy to handle the attribute values (Roger, Steven). If the branch name had an ", then it should have been escaped with &quot;. Yes, you're right, XML may change, have line breaks, etc. pp., an XML parser is definitely the better answer, but OP asked for grep and it could be that he knows what he is doing.

              – Freddy
              Jul 12 at 10:50















            7


















            With grep:



            grep -Pio 'name="andy".*branch="K[^"]*' file



            • -P enable perl regular expressions (PCRE)


            • -i ignore case


            • -o print only matched parts

            In the regex, the K is a zero-width lookbehind to match the part before the K, but to not include it in the match.






            share|improve this answer


























            • Ah, using Grep, I tried to do this way, but I guess my knowledge was very limited and I kept getting frustrated :$

              – John
              Jul 11 at 20:21











            • Wonderful solution, I learn every day.

              – Edward
              Jul 11 at 20:28






            • 4





              Parsing XML using grep is asking for trouble. What if the order of the attributes changes? What if there's some other (non-blah1) element that has similar attributes? What if the branch name includes "? Also, why -i? XML element and attribute names are case-sensitive. Now, all of these things are bugs waiting to surface at some point in the future. I recommend using the proper tool for the job; an XML parser.

              – marcelm
              Jul 12 at 8:21











            • The -i is taken from OP and could be handy to handle the attribute values (Roger, Steven). If the branch name had an ", then it should have been escaped with &quot;. Yes, you're right, XML may change, have line breaks, etc. pp., an XML parser is definitely the better answer, but OP asked for grep and it could be that he knows what he is doing.

              – Freddy
              Jul 12 at 10:50













            7














            7










            7









            With grep:



            grep -Pio 'name="andy".*branch="K[^"]*' file



            • -P enable perl regular expressions (PCRE)


            • -i ignore case


            • -o print only matched parts

            In the regex, the K is a zero-width lookbehind to match the part before the K, but to not include it in the match.






            share|improve this answer














            With grep:



            grep -Pio 'name="andy".*branch="K[^"]*' file



            • -P enable perl regular expressions (PCRE)


            • -i ignore case


            • -o print only matched parts

            In the regex, the K is a zero-width lookbehind to match the part before the K, but to not include it in the match.







            share|improve this answer













            share|improve this answer




            share|improve this answer










            answered Jul 11 at 20:19









            FreddyFreddy

            10.2k1 gold badge7 silver badges35 bronze badges




            10.2k1 gold badge7 silver badges35 bronze badges















            • Ah, using Grep, I tried to do this way, but I guess my knowledge was very limited and I kept getting frustrated :$

              – John
              Jul 11 at 20:21











            • Wonderful solution, I learn every day.

              – Edward
              Jul 11 at 20:28






            • 4





              Parsing XML using grep is asking for trouble. What if the order of the attributes changes? What if there's some other (non-blah1) element that has similar attributes? What if the branch name includes "? Also, why -i? XML element and attribute names are case-sensitive. Now, all of these things are bugs waiting to surface at some point in the future. I recommend using the proper tool for the job; an XML parser.

              – marcelm
              Jul 12 at 8:21











            • The -i is taken from OP and could be handy to handle the attribute values (Roger, Steven). If the branch name had an ", then it should have been escaped with &quot;. Yes, you're right, XML may change, have line breaks, etc. pp., an XML parser is definitely the better answer, but OP asked for grep and it could be that he knows what he is doing.

              – Freddy
              Jul 12 at 10:50

















            • Ah, using Grep, I tried to do this way, but I guess my knowledge was very limited and I kept getting frustrated :$

              – John
              Jul 11 at 20:21











            • Wonderful solution, I learn every day.

              – Edward
              Jul 11 at 20:28






            • 4





              Parsing XML using grep is asking for trouble. What if the order of the attributes changes? What if there's some other (non-blah1) element that has similar attributes? What if the branch name includes "? Also, why -i? XML element and attribute names are case-sensitive. Now, all of these things are bugs waiting to surface at some point in the future. I recommend using the proper tool for the job; an XML parser.

              – marcelm
              Jul 12 at 8:21











            • The -i is taken from OP and could be handy to handle the attribute values (Roger, Steven). If the branch name had an ", then it should have been escaped with &quot;. Yes, you're right, XML may change, have line breaks, etc. pp., an XML parser is definitely the better answer, but OP asked for grep and it could be that he knows what he is doing.

              – Freddy
              Jul 12 at 10:50
















            Ah, using Grep, I tried to do this way, but I guess my knowledge was very limited and I kept getting frustrated :$

            – John
            Jul 11 at 20:21





            Ah, using Grep, I tried to do this way, but I guess my knowledge was very limited and I kept getting frustrated :$

            – John
            Jul 11 at 20:21













            Wonderful solution, I learn every day.

            – Edward
            Jul 11 at 20:28





            Wonderful solution, I learn every day.

            – Edward
            Jul 11 at 20:28




            4




            4





            Parsing XML using grep is asking for trouble. What if the order of the attributes changes? What if there's some other (non-blah1) element that has similar attributes? What if the branch name includes "? Also, why -i? XML element and attribute names are case-sensitive. Now, all of these things are bugs waiting to surface at some point in the future. I recommend using the proper tool for the job; an XML parser.

            – marcelm
            Jul 12 at 8:21





            Parsing XML using grep is asking for trouble. What if the order of the attributes changes? What if there's some other (non-blah1) element that has similar attributes? What if the branch name includes "? Also, why -i? XML element and attribute names are case-sensitive. Now, all of these things are bugs waiting to surface at some point in the future. I recommend using the proper tool for the job; an XML parser.

            – marcelm
            Jul 12 at 8:21













            The -i is taken from OP and could be handy to handle the attribute values (Roger, Steven). If the branch name had an ", then it should have been escaped with &quot;. Yes, you're right, XML may change, have line breaks, etc. pp., an XML parser is definitely the better answer, but OP asked for grep and it could be that he knows what he is doing.

            – Freddy
            Jul 12 at 10:50





            The -i is taken from OP and could be handy to handle the attribute values (Roger, Steven). If the branch name had an ", then it should have been escaped with &quot;. Yes, you're right, XML may change, have line breaks, etc. pp., an XML parser is definitely the better answer, but OP asked for grep and it could be that he knows what he is doing.

            – Freddy
            Jul 12 at 10:50











            3


















            Using awk:



            awk '/name="andy"/ for (i=1;i<=NF;i++) if ($i ~ "branch=") sub(/branch=/, ""); gsub(/"/, ""); print $i ' input


            This will find a line containing name="andy" and then loop through each field in that line. If the field contains branch= we will remove branch= and all double quotes and print the remainder of the field.



            sub(/branch=/, "") is looking for a match of branch= and replacing it with "" (nothing)



            gsub is similar except it will replace globally (all occurances instead of just the first occurance).






            share|improve this answer




























            • Thank you so much, I will google to understand sub and gsub

              – John
              Jul 11 at 20:16











            • I wish I could rate this up but another answer is better as you mentioned.

              – John
              Jul 11 at 20:21











            • This is good but only works if branch is on the same line with name.

              – David Conrad
              Jul 12 at 16:28











            • @DavidConrad: Yes that is the requirement. If you notice, branch is on every line but OP only wants to return the value of branch that is on the same line as the name.

              – Jesse_b
              Jul 12 at 16:39











            • That isn't exactly the requirement, though, that's just the way this file happens to look. XML allows whitespace, so if you break the lines on spaces it will still work with the highest-upvoted answer but it will break with awk. It's a caveat people using this solution should be aware of. That said, this is a good quick-and-dirty solution, and I upvoted you.

              – David Conrad
              Jul 12 at 17:15















            3


















            Using awk:



            awk '/name="andy"/ for (i=1;i<=NF;i++) if ($i ~ "branch=") sub(/branch=/, ""); gsub(/"/, ""); print $i ' input


            This will find a line containing name="andy" and then loop through each field in that line. If the field contains branch= we will remove branch= and all double quotes and print the remainder of the field.



            sub(/branch=/, "") is looking for a match of branch= and replacing it with "" (nothing)



            gsub is similar except it will replace globally (all occurances instead of just the first occurance).






            share|improve this answer




























            • Thank you so much, I will google to understand sub and gsub

              – John
              Jul 11 at 20:16











            • I wish I could rate this up but another answer is better as you mentioned.

              – John
              Jul 11 at 20:21











            • This is good but only works if branch is on the same line with name.

              – David Conrad
              Jul 12 at 16:28











            • @DavidConrad: Yes that is the requirement. If you notice, branch is on every line but OP only wants to return the value of branch that is on the same line as the name.

              – Jesse_b
              Jul 12 at 16:39











            • That isn't exactly the requirement, though, that's just the way this file happens to look. XML allows whitespace, so if you break the lines on spaces it will still work with the highest-upvoted answer but it will break with awk. It's a caveat people using this solution should be aware of. That said, this is a good quick-and-dirty solution, and I upvoted you.

              – David Conrad
              Jul 12 at 17:15













            3














            3










            3









            Using awk:



            awk '/name="andy"/ for (i=1;i<=NF;i++) if ($i ~ "branch=") sub(/branch=/, ""); gsub(/"/, ""); print $i ' input


            This will find a line containing name="andy" and then loop through each field in that line. If the field contains branch= we will remove branch= and all double quotes and print the remainder of the field.



            sub(/branch=/, "") is looking for a match of branch= and replacing it with "" (nothing)



            gsub is similar except it will replace globally (all occurances instead of just the first occurance).






            share|improve this answer
















            Using awk:



            awk '/name="andy"/ for (i=1;i<=NF;i++) if ($i ~ "branch=") sub(/branch=/, ""); gsub(/"/, ""); print $i ' input


            This will find a line containing name="andy" and then loop through each field in that line. If the field contains branch= we will remove branch= and all double quotes and print the remainder of the field.



            sub(/branch=/, "") is looking for a match of branch= and replacing it with "" (nothing)



            gsub is similar except it will replace globally (all occurances instead of just the first occurance).







            share|improve this answer















            share|improve this answer




            share|improve this answer








            edited Jul 11 at 20:18

























            answered Jul 11 at 20:12









            Jesse_bJesse_b

            20.3k3 gold badges48 silver badges91 bronze badges




            20.3k3 gold badges48 silver badges91 bronze badges















            • Thank you so much, I will google to understand sub and gsub

              – John
              Jul 11 at 20:16











            • I wish I could rate this up but another answer is better as you mentioned.

              – John
              Jul 11 at 20:21











            • This is good but only works if branch is on the same line with name.

              – David Conrad
              Jul 12 at 16:28











            • @DavidConrad: Yes that is the requirement. If you notice, branch is on every line but OP only wants to return the value of branch that is on the same line as the name.

              – Jesse_b
              Jul 12 at 16:39











            • That isn't exactly the requirement, though, that's just the way this file happens to look. XML allows whitespace, so if you break the lines on spaces it will still work with the highest-upvoted answer but it will break with awk. It's a caveat people using this solution should be aware of. That said, this is a good quick-and-dirty solution, and I upvoted you.

              – David Conrad
              Jul 12 at 17:15

















            • Thank you so much, I will google to understand sub and gsub

              – John
              Jul 11 at 20:16











            • I wish I could rate this up but another answer is better as you mentioned.

              – John
              Jul 11 at 20:21











            • This is good but only works if branch is on the same line with name.

              – David Conrad
              Jul 12 at 16:28











            • @DavidConrad: Yes that is the requirement. If you notice, branch is on every line but OP only wants to return the value of branch that is on the same line as the name.

              – Jesse_b
              Jul 12 at 16:39











            • That isn't exactly the requirement, though, that's just the way this file happens to look. XML allows whitespace, so if you break the lines on spaces it will still work with the highest-upvoted answer but it will break with awk. It's a caveat people using this solution should be aware of. That said, this is a good quick-and-dirty solution, and I upvoted you.

              – David Conrad
              Jul 12 at 17:15
















            Thank you so much, I will google to understand sub and gsub

            – John
            Jul 11 at 20:16





            Thank you so much, I will google to understand sub and gsub

            – John
            Jul 11 at 20:16













            I wish I could rate this up but another answer is better as you mentioned.

            – John
            Jul 11 at 20:21





            I wish I could rate this up but another answer is better as you mentioned.

            – John
            Jul 11 at 20:21













            This is good but only works if branch is on the same line with name.

            – David Conrad
            Jul 12 at 16:28





            This is good but only works if branch is on the same line with name.

            – David Conrad
            Jul 12 at 16:28













            @DavidConrad: Yes that is the requirement. If you notice, branch is on every line but OP only wants to return the value of branch that is on the same line as the name.

            – Jesse_b
            Jul 12 at 16:39





            @DavidConrad: Yes that is the requirement. If you notice, branch is on every line but OP only wants to return the value of branch that is on the same line as the name.

            – Jesse_b
            Jul 12 at 16:39













            That isn't exactly the requirement, though, that's just the way this file happens to look. XML allows whitespace, so if you break the lines on spaces it will still work with the highest-upvoted answer but it will break with awk. It's a caveat people using this solution should be aware of. That said, this is a good quick-and-dirty solution, and I upvoted you.

            – David Conrad
            Jul 12 at 17:15





            That isn't exactly the requirement, though, that's just the way this file happens to look. XML allows whitespace, so if you break the lines on spaces it will still work with the highest-upvoted answer but it will break with awk. It's a caveat people using this solution should be aware of. That said, this is a good quick-and-dirty solution, and I upvoted you.

            – David Conrad
            Jul 12 at 17:15











            1


















            I think this works:



            $ grep -i 'name="andy" remote="origin" branch=".*"' <filename> | awk -F' ' 'print $5' | sed -E 's/branch="(.*)"/1/'
            master


            The awk part makes sure only branch="master" is returned, the sed part gives back what's between the double quotes with a reference (the 1 matches the part between the parentheses).



            Now I know there are a lot of people out here with far more knowledge on the art that is awk and sed, so I'm prepared for some criticism :-)






            share|improve this answer




























            • But I am passing in the file thought :$ Thanks a lot for the answer, I didn't think of using awk. I don't want to read each line, I kinda want to read the whole file and do this? Not possible?

              – John
              Jul 11 at 20:10












            • Editing my answer to show you how to pipe it through.

              – Edward
              Jul 11 at 20:11











            • This works, but like any solution that doesn't treat the XML as XML, it will stop working if the order of attributes changes or line breaks are inserted.

              – David Conrad
              Jul 13 at 18:05















            1


















            I think this works:



            $ grep -i 'name="andy" remote="origin" branch=".*"' <filename> | awk -F' ' 'print $5' | sed -E 's/branch="(.*)"/1/'
            master


            The awk part makes sure only branch="master" is returned, the sed part gives back what's between the double quotes with a reference (the 1 matches the part between the parentheses).



            Now I know there are a lot of people out here with far more knowledge on the art that is awk and sed, so I'm prepared for some criticism :-)






            share|improve this answer




























            • But I am passing in the file thought :$ Thanks a lot for the answer, I didn't think of using awk. I don't want to read each line, I kinda want to read the whole file and do this? Not possible?

              – John
              Jul 11 at 20:10












            • Editing my answer to show you how to pipe it through.

              – Edward
              Jul 11 at 20:11











            • This works, but like any solution that doesn't treat the XML as XML, it will stop working if the order of attributes changes or line breaks are inserted.

              – David Conrad
              Jul 13 at 18:05













            1














            1










            1









            I think this works:



            $ grep -i 'name="andy" remote="origin" branch=".*"' <filename> | awk -F' ' 'print $5' | sed -E 's/branch="(.*)"/1/'
            master


            The awk part makes sure only branch="master" is returned, the sed part gives back what's between the double quotes with a reference (the 1 matches the part between the parentheses).



            Now I know there are a lot of people out here with far more knowledge on the art that is awk and sed, so I'm prepared for some criticism :-)






            share|improve this answer
















            I think this works:



            $ grep -i 'name="andy" remote="origin" branch=".*"' <filename> | awk -F' ' 'print $5' | sed -E 's/branch="(.*)"/1/'
            master


            The awk part makes sure only branch="master" is returned, the sed part gives back what's between the double quotes with a reference (the 1 matches the part between the parentheses).



            Now I know there are a lot of people out here with far more knowledge on the art that is awk and sed, so I'm prepared for some criticism :-)







            share|improve this answer















            share|improve this answer




            share|improve this answer








            edited Jul 11 at 20:12

























            answered Jul 11 at 20:07









            EdwardEdward

            1,0531 gold badge7 silver badges16 bronze badges




            1,0531 gold badge7 silver badges16 bronze badges















            • But I am passing in the file thought :$ Thanks a lot for the answer, I didn't think of using awk. I don't want to read each line, I kinda want to read the whole file and do this? Not possible?

              – John
              Jul 11 at 20:10












            • Editing my answer to show you how to pipe it through.

              – Edward
              Jul 11 at 20:11











            • This works, but like any solution that doesn't treat the XML as XML, it will stop working if the order of attributes changes or line breaks are inserted.

              – David Conrad
              Jul 13 at 18:05

















            • But I am passing in the file thought :$ Thanks a lot for the answer, I didn't think of using awk. I don't want to read each line, I kinda want to read the whole file and do this? Not possible?

              – John
              Jul 11 at 20:10












            • Editing my answer to show you how to pipe it through.

              – Edward
              Jul 11 at 20:11











            • This works, but like any solution that doesn't treat the XML as XML, it will stop working if the order of attributes changes or line breaks are inserted.

              – David Conrad
              Jul 13 at 18:05
















            But I am passing in the file thought :$ Thanks a lot for the answer, I didn't think of using awk. I don't want to read each line, I kinda want to read the whole file and do this? Not possible?

            – John
            Jul 11 at 20:10






            But I am passing in the file thought :$ Thanks a lot for the answer, I didn't think of using awk. I don't want to read each line, I kinda want to read the whole file and do this? Not possible?

            – John
            Jul 11 at 20:10














            Editing my answer to show you how to pipe it through.

            – Edward
            Jul 11 at 20:11





            Editing my answer to show you how to pipe it through.

            – Edward
            Jul 11 at 20:11













            This works, but like any solution that doesn't treat the XML as XML, it will stop working if the order of attributes changes or line breaks are inserted.

            – David Conrad
            Jul 13 at 18:05





            This works, but like any solution that doesn't treat the XML as XML, it will stop working if the order of attributes changes or line breaks are inserted.

            – David Conrad
            Jul 13 at 18:05


















            draft saved

            draft discarded















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f529670%2fextract-an-attribute-value-from-xml%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown









            Popular posts from this blog

            Distance measures on a map of a game The 2019 Stack Overflow Developer Survey Results Are Inmin distance in a graphShortest distance path on contour plotHow to plot a tilted map?Finding points outside of a diskDelaunay link distanceAnnulus from GeoDisks: drawing a ring on a mapNegative Correlation DistanceFind distance along a path (GPS coordinates)Finding position at given distance in a GeoPathMathematics behind distance estimation using camera

            How to get a smooth, uniform ParametricPlot of a 2D Region?How to plot a complicated Region?How to exclude a region from ParametricPlotHow discretize a region placing vertices on a specific non-uniform gridHow to transform a Plot or a ParametricPlot into a RegionHow can I get a smooth plot of a bounded region?Smooth ParametricPlot3D with RegionFunction?Smooth border of a region ParametricPlotSmooth region boundarySmooth region plot from list of pointsGet minimum y of a certain x in a region

            Genealogie vun de Merowenger Vum Merowech bis zum Chilperich I. | Navigatiounsmenü