Tag Info

New answers tagged

0

Here is a proper answer using xmlstarlet. This is a tool used for xml parsing and editing. First of all, install this package on your system. If you're on a Debian-based system, then do: sudo apt-get install xmlstarlet Now, first we read the value of base64 encoded string then we decode this string then we modify the corresponding tag value Here is ...


0

Please don't use awk for XML parsing. This is a bad idea, because XML supports things like line feeding, indentation, line wrapping on attributes and unary tags - all of which means that semantically identical XML breaks when you use a line/field/regex oriented approach. So I would strongly suggest using an XML tool to build your XML - and as an example: ...


1

I'll say what I always do. Please NEVER use regular expressions to parse XML. It's bad news. XML has some various formatting which means semantically identical XML will match or not match certain regular expressions. Simple things like line wrapping, unary tags, etc. This means you create brittle code, which one day might mysteriously break because of an ...


1

Compact Script Assuming the xml is in file.xml, just do: sed -r 's/("base64">)([[:graph:]]+)/\1'"`grep -oP '"base64">\K[[:graph:]]+' test | base64 -d`"'/g' file.xml This is a compact regex, which will do the task. Let me break it down and explain. Break Down First I select the base64 string using grep and decode it: grep -oP ...


0

You could use the fantastic BeautifulSoup Python library. This code sample retrieves an XML file from a URL given as input argument to the script, parses it using LXML and then pretty prints it. #!/usr/bin/env python import sys import urllib2 import bs4 soup = bs4.BeautifulSoup(urllib2.urlopen(sys.argv[1]), ["lxml", "xml"]) print(soup.prettify()) If you ...


0

I would strongly urge away from grep/sed - they don't work for XML. But fortunately, perl and XML::Twig have all sorts of magic for reformatting and extracting values as you wish. get_xpath works nicely for extracting a value, or you have twig_handlers to handle elements depending on use case. (Or just iterate using children or similar). But anyway - to ...


2

You could first escape the & when found in an entity, and then substitute the remaining ones. Like: LC_ALL=C sed 's/_/_u/g; # use _ as an escape character. Here escape itself s/&\([[:alpha:]][[:alnum:]]*;\)/_a\1/g; # replace & with _a when in entities s/&\(#[0-9]\{1,8\};\)/_a\1/g; # Ӓ case ...


0

I hope it's not that you are missing the closing single quote for awk - hopefully typo! Also remember you can just print "\n" instead of lots of separate print commands (or even use a semicolon to separate them).


1

You can try something like awk 'BEGIN{FS = "\n";RS = "\n\n"; print " "} { print "<candidate>" } { print "<name>"$1"</name>" } { print "<marks>"$2"</marks>" } { print "<subject>"$3"</subject>" } { print "</candidate>" } {print " " }' input.txt > candiatefinaloutput.xml And there is what i get: ...



Top 50 recent answers are included