Using AWK to parse from XML The Next CEO of Stack OverflowBEGIN and END with the awk commandParse/Manipulate in awkUsing getline with NR in awkhow to use n( and )n as delimiter in awkHow to print same lines multiple times with variables changed using Sed/Awk/ anything?Extract and delete the first occurrence of the XML tag multiple timesUsing awk as simple template engine, how to modify output of last line from getlinecompare and print the values in two arrays using awkCount the number of occurrences of a substring in a stringCompare dates using awk in bash
Reshaping json / reparing json inside shell script (remove trailing comma)
How to get the last not-null value in an ordered column of a huge table?
Why am I getting "Static method cannot be referenced from a non static context: String String.valueOf(Object)"?
What was Carter Burke's job for "the company" in Aliens?
Could a dragon use its wings to swim?
Can someone explain this formula for calculating Manhattan distance?
Is it convenient to ask the journal's editor for two additional days to complete a review?
Decide between Polyglossia and Babel for LuaLaTeX in 2019
Can Sneak Attack be used when hitting with an improvised weapon?
How to use ReplaceAll on an expression that contains a rule
What day is it again?
In the "Harry Potter and the Order of the Phoenix" video game, what potion is used to sabotage Umbridge's speakers?
Help! I cannot understand this game’s notations!
How did Beeri the Hittite come up with naming his daughter Yehudit?
Does destroying a Lich's phylactery destroy the soul within it?
Getting Stale Gas Out of a Gas Tank w/out Dropping the Tank
Does higher Oxidation/ reduction potential translate to higher energy storage in battery?
What happened in Rome, when the western empire "fell"?
Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?
What is the difference between "hamstring tendon" and "common hamstring tendon"?
Calculate the Mean mean of two numbers
"Eavesdropping" vs "Listen in on"
Physiological effects of huge anime eyes
Is there a reasonable and studied concept of reduction between regular languages?
Using AWK to parse from XML
The Next CEO of Stack OverflowBEGIN and END with the awk commandParse/Manipulate in awkUsing getline with NR in awkhow to use n( and )n as delimiter in awkHow to print same lines multiple times with variables changed using Sed/Awk/ anything?Extract and delete the first occurrence of the XML tag multiple timesUsing awk as simple template engine, how to modify output of last line from getlinecompare and print the values in two arrays using awkCount the number of occurrences of a substring in a stringCompare dates using awk in bash
I need just the first instance of an ANSI formatted date (YYYY-MM-DD) from the string shown below.
PosMntReq ReqId="XXXXX" TxnTyp="4" Actn="1" BizDt="2019-03-27" TxnTm="2019-03-27T10:41:13" AdjTyp="3" SetSesID="EOD">
The command I wrote with awk does not provide the date
v_business_date=$(awk -F= 'NR==4 print $5' XMLCD02)
bash-4.2$ echo $v_business_date
"2019-03-27" TxnTm
The line with the date will be in the file many times; I want the first occurrence of this line.
shell-script awk xml
add a comment |
I need just the first instance of an ANSI formatted date (YYYY-MM-DD) from the string shown below.
PosMntReq ReqId="XXXXX" TxnTyp="4" Actn="1" BizDt="2019-03-27" TxnTm="2019-03-27T10:41:13" AdjTyp="3" SetSesID="EOD">
The command I wrote with awk does not provide the date
v_business_date=$(awk -F= 'NR==4 print $5' XMLCD02)
bash-4.2$ echo $v_business_date
"2019-03-27" TxnTm
The line with the date will be in the file many times; I want the first occurrence of this line.
shell-script awk xml
Are you parsing an XML file?
– glenn jackman
2 days ago
Yes..This is an XML file.
– user313150
2 days ago
5
Then you'll want to use an XML parser. xmlstarlet is a good choice for command line work. This simplifies your work to extract value, as you basically just need the XPath to that attribute.
– glenn jackman
2 days ago
2
This Stack Overflow page provides guidance on how to ask a good question: How to create a Minimal, Complete, and Verifiable example
– glenn jackman
2 days ago
add a comment |
I need just the first instance of an ANSI formatted date (YYYY-MM-DD) from the string shown below.
PosMntReq ReqId="XXXXX" TxnTyp="4" Actn="1" BizDt="2019-03-27" TxnTm="2019-03-27T10:41:13" AdjTyp="3" SetSesID="EOD">
The command I wrote with awk does not provide the date
v_business_date=$(awk -F= 'NR==4 print $5' XMLCD02)
bash-4.2$ echo $v_business_date
"2019-03-27" TxnTm
The line with the date will be in the file many times; I want the first occurrence of this line.
shell-script awk xml
I need just the first instance of an ANSI formatted date (YYYY-MM-DD) from the string shown below.
PosMntReq ReqId="XXXXX" TxnTyp="4" Actn="1" BizDt="2019-03-27" TxnTm="2019-03-27T10:41:13" AdjTyp="3" SetSesID="EOD">
The command I wrote with awk does not provide the date
v_business_date=$(awk -F= 'NR==4 print $5' XMLCD02)
bash-4.2$ echo $v_business_date
"2019-03-27" TxnTm
The line with the date will be in the file many times; I want the first occurrence of this line.
shell-script awk xml
shell-script awk xml
edited 2 days ago
Jeff Schaller♦
44.4k1162143
44.4k1162143
asked 2 days ago
user313150user313150
54
54
Are you parsing an XML file?
– glenn jackman
2 days ago
Yes..This is an XML file.
– user313150
2 days ago
5
Then you'll want to use an XML parser. xmlstarlet is a good choice for command line work. This simplifies your work to extract value, as you basically just need the XPath to that attribute.
– glenn jackman
2 days ago
2
This Stack Overflow page provides guidance on how to ask a good question: How to create a Minimal, Complete, and Verifiable example
– glenn jackman
2 days ago
add a comment |
Are you parsing an XML file?
– glenn jackman
2 days ago
Yes..This is an XML file.
– user313150
2 days ago
5
Then you'll want to use an XML parser. xmlstarlet is a good choice for command line work. This simplifies your work to extract value, as you basically just need the XPath to that attribute.
– glenn jackman
2 days ago
2
This Stack Overflow page provides guidance on how to ask a good question: How to create a Minimal, Complete, and Verifiable example
– glenn jackman
2 days ago
Are you parsing an XML file?
– glenn jackman
2 days ago
Are you parsing an XML file?
– glenn jackman
2 days ago
Yes..This is an XML file.
– user313150
2 days ago
Yes..This is an XML file.
– user313150
2 days ago
5
5
Then you'll want to use an XML parser. xmlstarlet is a good choice for command line work. This simplifies your work to extract value, as you basically just need the XPath to that attribute.
– glenn jackman
2 days ago
Then you'll want to use an XML parser. xmlstarlet is a good choice for command line work. This simplifies your work to extract value, as you basically just need the XPath to that attribute.
– glenn jackman
2 days ago
2
2
This Stack Overflow page provides guidance on how to ask a good question: How to create a Minimal, Complete, and Verifiable example
– glenn jackman
2 days ago
This Stack Overflow page provides guidance on how to ask a good question: How to create a Minimal, Complete, and Verifiable example
– glenn jackman
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
Making some assumptions about your data:
$ cat file.xml
<root>
<node1>
<node2 ReqId="XXXXX" TxnTyp="4" Actn="1" BizDt="2019-03-27" TxnTm="2019-03-27T10:41:13" AdjTyp="3" SetSesID="EOD">
</node2>
</node1>
</root>
$ txn_tm=$( xmlstarlet sel -t -v '//node2/@TxnTm' file.xml )
$ echo "$txn_tm"
2019-03-27T10:41:13
If you want to select the node based on the ReqId, then we need to adjust the xpath: return the "TxnTm" attribute value for any node with the specified "ReqId" attribute.
xmlstarlet sel -t -v '//*[@ReqId = "XXXXX"]/@TxnTm' -n file.xml
It's likely that they want to useReqId
in the selection.
– Kusalananda♦
2 days ago
Argh, not seen that was XML :)
– Gilles Quenot
2 days ago
add a comment |
Don't use regular expressions to parse XML. That way lies madness. But if you insist on a descent into madness and ponies:
sed -En '/TxnTm=/s/^.*TxnTm="//;s/T[0-9:]+".*$//;p;q;' < inputfile
well above command is printing all occurrences of date whereas I want to print it only once.
– user313150
2 days ago
2
@user313150 Please update the question with a complete and well formed XML document. You only gave us a partial snippet. We have no way of knowing what the rest of the document looks like or if there are more date attributes elsewhere.
– Kusalananda♦
2 days ago
1
This works for the example input given. If that input was insufficient to advise the answer, this is a case of GIGO, and also a case of don't parse XML with regular expressions.
– DopeGhoti
2 days ago
I have edited the command to stop at the first match.
– DopeGhoti
2 days ago
Thanks a lot ..
– user313150
2 days ago
add a comment |
A simpler option using just grep
, albeit again with the advisory that using regular expression tools to parse XML or XHTML is a one-way ticket to sadness-town:
$ grep -m1 -oE '"[0-9]4-[0-9]2-[0-9]2"' input
"2019-03-27"
add a comment |
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/3.0/"u003ecc by-sa 3.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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f509455%2fusing-awk-to-parse-from-xml%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Making some assumptions about your data:
$ cat file.xml
<root>
<node1>
<node2 ReqId="XXXXX" TxnTyp="4" Actn="1" BizDt="2019-03-27" TxnTm="2019-03-27T10:41:13" AdjTyp="3" SetSesID="EOD">
</node2>
</node1>
</root>
$ txn_tm=$( xmlstarlet sel -t -v '//node2/@TxnTm' file.xml )
$ echo "$txn_tm"
2019-03-27T10:41:13
If you want to select the node based on the ReqId, then we need to adjust the xpath: return the "TxnTm" attribute value for any node with the specified "ReqId" attribute.
xmlstarlet sel -t -v '//*[@ReqId = "XXXXX"]/@TxnTm' -n file.xml
It's likely that they want to useReqId
in the selection.
– Kusalananda♦
2 days ago
Argh, not seen that was XML :)
– Gilles Quenot
2 days ago
add a comment |
Making some assumptions about your data:
$ cat file.xml
<root>
<node1>
<node2 ReqId="XXXXX" TxnTyp="4" Actn="1" BizDt="2019-03-27" TxnTm="2019-03-27T10:41:13" AdjTyp="3" SetSesID="EOD">
</node2>
</node1>
</root>
$ txn_tm=$( xmlstarlet sel -t -v '//node2/@TxnTm' file.xml )
$ echo "$txn_tm"
2019-03-27T10:41:13
If you want to select the node based on the ReqId, then we need to adjust the xpath: return the "TxnTm" attribute value for any node with the specified "ReqId" attribute.
xmlstarlet sel -t -v '//*[@ReqId = "XXXXX"]/@TxnTm' -n file.xml
It's likely that they want to useReqId
in the selection.
– Kusalananda♦
2 days ago
Argh, not seen that was XML :)
– Gilles Quenot
2 days ago
add a comment |
Making some assumptions about your data:
$ cat file.xml
<root>
<node1>
<node2 ReqId="XXXXX" TxnTyp="4" Actn="1" BizDt="2019-03-27" TxnTm="2019-03-27T10:41:13" AdjTyp="3" SetSesID="EOD">
</node2>
</node1>
</root>
$ txn_tm=$( xmlstarlet sel -t -v '//node2/@TxnTm' file.xml )
$ echo "$txn_tm"
2019-03-27T10:41:13
If you want to select the node based on the ReqId, then we need to adjust the xpath: return the "TxnTm" attribute value for any node with the specified "ReqId" attribute.
xmlstarlet sel -t -v '//*[@ReqId = "XXXXX"]/@TxnTm' -n file.xml
Making some assumptions about your data:
$ cat file.xml
<root>
<node1>
<node2 ReqId="XXXXX" TxnTyp="4" Actn="1" BizDt="2019-03-27" TxnTm="2019-03-27T10:41:13" AdjTyp="3" SetSesID="EOD">
</node2>
</node1>
</root>
$ txn_tm=$( xmlstarlet sel -t -v '//node2/@TxnTm' file.xml )
$ echo "$txn_tm"
2019-03-27T10:41:13
If you want to select the node based on the ReqId, then we need to adjust the xpath: return the "TxnTm" attribute value for any node with the specified "ReqId" attribute.
xmlstarlet sel -t -v '//*[@ReqId = "XXXXX"]/@TxnTm' -n file.xml
edited 2 days ago
answered 2 days ago
glenn jackmanglenn jackman
52.9k573114
52.9k573114
It's likely that they want to useReqId
in the selection.
– Kusalananda♦
2 days ago
Argh, not seen that was XML :)
– Gilles Quenot
2 days ago
add a comment |
It's likely that they want to useReqId
in the selection.
– Kusalananda♦
2 days ago
Argh, not seen that was XML :)
– Gilles Quenot
2 days ago
It's likely that they want to use
ReqId
in the selection.– Kusalananda♦
2 days ago
It's likely that they want to use
ReqId
in the selection.– Kusalananda♦
2 days ago
Argh, not seen that was XML :)
– Gilles Quenot
2 days ago
Argh, not seen that was XML :)
– Gilles Quenot
2 days ago
add a comment |
Don't use regular expressions to parse XML. That way lies madness. But if you insist on a descent into madness and ponies:
sed -En '/TxnTm=/s/^.*TxnTm="//;s/T[0-9:]+".*$//;p;q;' < inputfile
well above command is printing all occurrences of date whereas I want to print it only once.
– user313150
2 days ago
2
@user313150 Please update the question with a complete and well formed XML document. You only gave us a partial snippet. We have no way of knowing what the rest of the document looks like or if there are more date attributes elsewhere.
– Kusalananda♦
2 days ago
1
This works for the example input given. If that input was insufficient to advise the answer, this is a case of GIGO, and also a case of don't parse XML with regular expressions.
– DopeGhoti
2 days ago
I have edited the command to stop at the first match.
– DopeGhoti
2 days ago
Thanks a lot ..
– user313150
2 days ago
add a comment |
Don't use regular expressions to parse XML. That way lies madness. But if you insist on a descent into madness and ponies:
sed -En '/TxnTm=/s/^.*TxnTm="//;s/T[0-9:]+".*$//;p;q;' < inputfile
well above command is printing all occurrences of date whereas I want to print it only once.
– user313150
2 days ago
2
@user313150 Please update the question with a complete and well formed XML document. You only gave us a partial snippet. We have no way of knowing what the rest of the document looks like or if there are more date attributes elsewhere.
– Kusalananda♦
2 days ago
1
This works for the example input given. If that input was insufficient to advise the answer, this is a case of GIGO, and also a case of don't parse XML with regular expressions.
– DopeGhoti
2 days ago
I have edited the command to stop at the first match.
– DopeGhoti
2 days ago
Thanks a lot ..
– user313150
2 days ago
add a comment |
Don't use regular expressions to parse XML. That way lies madness. But if you insist on a descent into madness and ponies:
sed -En '/TxnTm=/s/^.*TxnTm="//;s/T[0-9:]+".*$//;p;q;' < inputfile
Don't use regular expressions to parse XML. That way lies madness. But if you insist on a descent into madness and ponies:
sed -En '/TxnTm=/s/^.*TxnTm="//;s/T[0-9:]+".*$//;p;q;' < inputfile
edited 2 days ago
answered 2 days ago
DopeGhotiDopeGhoti
46.7k56190
46.7k56190
well above command is printing all occurrences of date whereas I want to print it only once.
– user313150
2 days ago
2
@user313150 Please update the question with a complete and well formed XML document. You only gave us a partial snippet. We have no way of knowing what the rest of the document looks like or if there are more date attributes elsewhere.
– Kusalananda♦
2 days ago
1
This works for the example input given. If that input was insufficient to advise the answer, this is a case of GIGO, and also a case of don't parse XML with regular expressions.
– DopeGhoti
2 days ago
I have edited the command to stop at the first match.
– DopeGhoti
2 days ago
Thanks a lot ..
– user313150
2 days ago
add a comment |
well above command is printing all occurrences of date whereas I want to print it only once.
– user313150
2 days ago
2
@user313150 Please update the question with a complete and well formed XML document. You only gave us a partial snippet. We have no way of knowing what the rest of the document looks like or if there are more date attributes elsewhere.
– Kusalananda♦
2 days ago
1
This works for the example input given. If that input was insufficient to advise the answer, this is a case of GIGO, and also a case of don't parse XML with regular expressions.
– DopeGhoti
2 days ago
I have edited the command to stop at the first match.
– DopeGhoti
2 days ago
Thanks a lot ..
– user313150
2 days ago
well above command is printing all occurrences of date whereas I want to print it only once.
– user313150
2 days ago
well above command is printing all occurrences of date whereas I want to print it only once.
– user313150
2 days ago
2
2
@user313150 Please update the question with a complete and well formed XML document. You only gave us a partial snippet. We have no way of knowing what the rest of the document looks like or if there are more date attributes elsewhere.
– Kusalananda♦
2 days ago
@user313150 Please update the question with a complete and well formed XML document. You only gave us a partial snippet. We have no way of knowing what the rest of the document looks like or if there are more date attributes elsewhere.
– Kusalananda♦
2 days ago
1
1
This works for the example input given. If that input was insufficient to advise the answer, this is a case of GIGO, and also a case of don't parse XML with regular expressions.
– DopeGhoti
2 days ago
This works for the example input given. If that input was insufficient to advise the answer, this is a case of GIGO, and also a case of don't parse XML with regular expressions.
– DopeGhoti
2 days ago
I have edited the command to stop at the first match.
– DopeGhoti
2 days ago
I have edited the command to stop at the first match.
– DopeGhoti
2 days ago
Thanks a lot ..
– user313150
2 days ago
Thanks a lot ..
– user313150
2 days ago
add a comment |
A simpler option using just grep
, albeit again with the advisory that using regular expression tools to parse XML or XHTML is a one-way ticket to sadness-town:
$ grep -m1 -oE '"[0-9]4-[0-9]2-[0-9]2"' input
"2019-03-27"
add a comment |
A simpler option using just grep
, albeit again with the advisory that using regular expression tools to parse XML or XHTML is a one-way ticket to sadness-town:
$ grep -m1 -oE '"[0-9]4-[0-9]2-[0-9]2"' input
"2019-03-27"
add a comment |
A simpler option using just grep
, albeit again with the advisory that using regular expression tools to parse XML or XHTML is a one-way ticket to sadness-town:
$ grep -m1 -oE '"[0-9]4-[0-9]2-[0-9]2"' input
"2019-03-27"
A simpler option using just grep
, albeit again with the advisory that using regular expression tools to parse XML or XHTML is a one-way ticket to sadness-town:
$ grep -m1 -oE '"[0-9]4-[0-9]2-[0-9]2"' input
"2019-03-27"
answered 2 days ago
DopeGhotiDopeGhoti
46.7k56190
46.7k56190
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f509455%2fusing-awk-to-parse-from-xml%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Are you parsing an XML file?
– glenn jackman
2 days ago
Yes..This is an XML file.
– user313150
2 days ago
5
Then you'll want to use an XML parser. xmlstarlet is a good choice for command line work. This simplifies your work to extract value, as you basically just need the XPath to that attribute.
– glenn jackman
2 days ago
2
This Stack Overflow page provides guidance on how to ask a good question: How to create a Minimal, Complete, and Verifiable example
– glenn jackman
2 days ago