Score:0

How to use bash/sed to extract XML attribute value

in flag

I have the following grep command piped into sed to find an element name attribute and store the sed result into a name variable.

 name=$(grep -E "<element.*name=.*/>" "$F" | sed -e "s/.*<element.*name=\(.*\)\/>.*?/\1/")

Sample Data -

<element name="Barium"/>

Desired Output -

Barium

Actual Output -

<element name="Barium"/>

I'm a little confused on how to get the sed command to get rid of the excess.

(I know xmllint would make this easy, but can't use it for this)

Score:0
cn flag

You don't need sed for this:

grep -Po '(?<=<element name=")[^"]+' <file>

Or

grep -Po '<element name="\K[^"]+' <file>
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.