Tag Info

Hot answers tagged

10

You can use groups, e.g.: $ sed -i 's/<\(.*\)>\(.*\)<.*>/<column name="\1">\2<\/column>/g' filename.xml Probably the most confusing part about REs is that there are various syntactic flavors. For example sed and vim use basic regular expressions where you have to quote () to get their meta-meaning. With extended regular ...


9

I've had the most luck with this: :%!xmllint --format % It's strict about your tags, though, so it will error out if your opening and closing tags don't match. It also adds an XML declaration at the top of your file, if you don't have one as well. This page recommends the following, although I can't get it to work: :set filetype=xml :filetype indent on ...


7

This can be done from find directly using -exec: find . -name "*.xml" -type f -exec xmllint --output '{}' --format '{}' \; Whats passed to -exec will be invoked once per file found with the template parameters {} being replaced with the current file name. The \; on the end of the find command just terminates the line. The use of xargs isn't really ...


6

Install the matchit plugin (see :help matchit-install for instructions). Make sure automatic file-type detection and plugin-loading is enabled (:filetype plugin on). Henceforth, whenever you edit a file detected as xml or html or some other tag-based markup language, the combination of the matchit plugin and the filetype plugin files will allow the % ...


5

sed/awk are really about regular expressions. check this answer on stackoverflow why parsing HTML/XML with regular expressions is a bad idea. for XML you really need to build a DOM of the document and then find your information. there are cmdline tools like xmlstar that allow you to get information out of XML-documents. but do not try using sed/awk to ...


4

This discussion is enlightening. At the very least, even if not ideal, you should be able to do: xmllint --xpath "//*[local-name()='product_version']/*[local-name()='name']/text()" file.xml Or use xmlstarlet instead: xmlstarlet sel -t -v //swid:product_version/swid:name file.xml


4

Using Python (2.7) with standard modules: file test.xml: <Container> <Placemark> <KeepMe/> </Placemark> <Placemark> <styleUrl>#m_ylw-pushpin330</styleUrl> <LineString> <tessellate>1</tessellate> <coordinates> 0.0000000000000,0.0000000000000,0 ...


4

Here another short solution with sed and ed. It modify the XML file inplace. Ignore the output to the console. sed -e 's#.*#/<headTag>/i\n&\n.\n//\nw#' PATH_TO_LIST_FILE | ed PATH_TO_XML_FILE The sed commands line writes following Ed commands for each line in the list file: /<headTag>/i # search for tag and insert before ...


3

Given this test file: start <Placemark> <tessellate>1</tessellate> </Placemark> middle1 <Placemark> </Placemark> middle2 <Placemark> <tessellate>1</tessellate> </Placemark> end If you do perl -0 -pe 's|<Placemark>.*?<tessellate>.*?</Placemark>||gs' like you ...


3

With xmlstarlet: xmlstarlet ed -d '//Placemark[.//tessellate]' < myplaces.kml And as kml uses namespaces, you have to define it first (see the xmlstarlet documentation) xmlstarlet ed -N 'ns=http://www.opengis.net/kml/2.2' -d '//ns:Placemark[.//ns:tessellate]' With perl, you'd need to process the file as a whole (not line by line) and add the s flag ...


3

jsawk will probably do what you need: https://github.com/micha/jsawk Edit: However I found jshon to work much better. Here is an example: curl 'http://twitter.com/users/username.json' | jshon -e "location" Outputs: "new hampshire"


3

As Mat said, indentation (and whitespace in general) is not important in XML files. This: <one><tags></tags></one> Is exactly equivalent to: <one> <tags> </tags> </one> But this will work while preserving indentation: $ cat myfile.xml <tags> </tags> $ sed '/<one>/ a\ <tags>\ ...


3

The following works when run against your test data: { read -r host; read -r username; read -r password; read -r dbname; } \ < <(xmlstarlet sel -t -m /config/global/resources/default_setup/connection \ -v ./host -n \ -v ./username -n \ -v ./password -n \ -v ./dbname -n) This puts the content into variables host, username, ...


2

Here's an awk script that attempts to produce decent indentation. It assumes the input is well-formed (e.g. the line before 3.2.2 must be 3.1 or 3.1.something, not 3). #! /usr/bin/awk -f BEGIN { printf "(bookmarks"; depth = 1; } { level = split($1, s, "."); while (level < depth) {--depth; printf ")";} print ""; depth = level + 1; ...


2

Sounds like your log files are actually an XML format. If that's the case, then what you REALLY should be doing is using an XML parser. Read this famous StackOverflow answer if you want some more info. Based on your description, a SAX-based parser is probably your best bet: these are stream-oriented parsers and don't require you to load the whole file in ...


2

for xml there is http://xmlstar.sourceforge.net/ XMLStarlet is a set of command line utilities (tools) which can be used to transform, query, validate, and edit XML documents and files using simple set of shell commands in similar way it is done for plain text files using UNIX grep, sed, awk, diff, patch, join, etc commands. you can also ...


2

It's not free, but oXygen has this feature, and runs on all three major platforms. (It's Java-based.) They have a screencast demo of the feature. You can get oXygen in both a standalone version and one that runs in Eclipse, which is nice since you may already be using Eclipse for developing the parts of the system that consume or produce the XML.


2

As far as I know the | separator can be used only on entire paths: echo 'cat /root/child/@attr1|/root/child/@attr2|/root/child/@attr3' | xmllint --shell data.xml (As // means at any depth, “//root” puts the parser to pointless extra work. Assuming your sample XML looks has similar structure as the real one (so root is indeed the XML's root node), better ...


2

I typically attack these problems with a layer of indirection. Write a shell script that does what you want, and call that. I'd suggest as a start #! /bin/sh for file do xmllint --format $file > $file.tmp && mv $file.tmp $file done The try it out on a file or two by hand, then you can replace it in the xargs find . -name "*.xml" -type f | ...


2

A simple solution for simple cases - see my comment: echo "<g:gtin>31806831001</g:gtin>" | sed 's|<g:gtin>.*</g:gtin>|<g:gtin></g:gtin>|' Result: <g:gtin></g:gtin> It depends on the assumption that start and endtag are on the same line, and not more than one tag is on that line. Since xml files are ...


2

Why don't you just split it in the files you propose and then just cat them all together? cat rc-something.xml rc.keyboard.xml rc.mouse.xml > rc.xml The only problem is that you will need to cat them each time you modify one of the individual files, but that should be trivial..


2

I did not test these validators, but from the top of my mind / little search: XMLStarlet - can be used for other things as well msv - Sun multi schema validator HaXML - haskell xml tools contains command line utilities (one is a validator) xsltproc should also verify documents at startup There are plenty more option as most utilities will automatically ...


2

You can use sed for the same sed -i.bkp 5'i'"s/search/replace/" inputfile -i.bkp take backup as inpufile.bkp and edit original file 5'i' go to line 5 s/ search and replace also you can search specific word say 'NAME' and in same line search 'RAM' and replace with 'SHYAM' , see example below sed -e '/NAME/s/RAM/SHYAM/g' filename reference link ...


2

You might like to try dtdgen, a program I wrote many years ago to generate a DTD for a document. It not only tells you whether a large file is well-formed, it also tells you what's in it (I wrote it because I wanted to know both).


2

Using bash and xmllint (as given by the tags): xmllint --version # xmllint: using libxml version 20703 # Note: Newer versions of libxml / xmllint have a --xpath option which # makes it possible to use xpath expressions directly as arguments. # --xpath also enables precise output in contrast to the --shell & sed approaches below. #xmllint --help ...


2

The xml2 command will convert XML to flat text. $ cat foo.xml <?xml version='1.0'?> <doc name="document"> <object name="foo"> <key name="bar">baz</key> </object> <definition name="ticketOpen" extends="ticketUpdate"> <put name="title" value="Open Ticket" /> <put name="mainForm" value="open.jsp" /> ...


2

The following XQuery should give you the desired output : for $x in (/content/element1,/content/element2) return $x/text() For example, with an XQuery interpreter such as XQilla and an input file like <?xml version="1.0" ?> <content> <element1>truc</element1> <dontcare>blah</dontcare> ...


1

Here's one way to do it. I just put your output into a file called sample.txt to make it easier to test, you can just append my commands to the end of your echo command: sample.txt Folder="FOLDER1M\1" File="R1.txt" Folder="FOLDER1M\2" File="R2.txt" Folder="FOLDER2M\3" File="R3.txt" command % cat sample.txt | sed 'h;s/.*//;G;N;s/\n//g' | sed ...


1

I would use awk if the format doesn't diverge from the sample you provided: awk -F'[<>="[:blank:]]+' ' $2 == "domain" {group = $(NF-1)} !(group == "group1" && $2 == "node" && $(NF-1) == "PQR") ' < dest.xml > new-dest.xml to remove that "PQR" node in the "group1" domain. $ diff -u dest.xml new-dest.xml --- dest.xml ...


1

As mentioned by @donothingsuccesfully modifying xml with simple text replacement when you have requirements like tag xyz inside tag abc is generally a bad idea unless you have a very strict, known format of the input xml file, which is rarely the case. You need something that understands xml. xml-sed from xml-coreutils allows you to run sed commands on an ...



Only top voted, non community-wiki answers of a minimum length are eligible