Tag Info

Hot answers tagged

13

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 ...


11

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 ...


10

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 ...


8

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 % ...


7

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


7

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 ...


7

I'd use an XML handling tool like xsh: open subtitles.xml ; for /transcript/Item { echo position() ; echo @from '-->' (@from + @duration) ; echo text() ; } Output: 1 1.16 --> 5.84 (Dong-hyuk is coming to see you now.) 2 5.92 --> 6.92 It's cold. 3 9.04 --> 11.92 - Hello. - Hello. 4 12.2 --> 13.96 You're busy as always. 5 ...


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 ...


5

using awk : awk '{print $4,$5,$6,$7,$8,$9,$1,$2,$3}' log_file you can do it directly from you log file like this: awk '{printf("<wpt %s%s\"%s\" %s%s\"%s\">\n<time>%s %s %s</time>\n</wpt>\n",$4,$5,substr($6,0,length($6)),$7,$8,$9,$1,$2,$3)}' log_file output: <wpt lat="41.858657" lon="-91.345142"> <time>11-06-2014 - ...


4

I guess Perl, Python or Ruby modules can be used successfully for that. And any of these can be used for scripting.


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 ...


4

Try: xmllint --xpath '//tag[position()<=2]' file.xml Or: xmlstarlet sel -t -c '//tag[position()<=2]' file.xml Or: xmlstarlet sel -t -m '//tag[position()<=2]' -c . -n file.xml If you wanted to do it with sed only you could do something like: sed -n ' 1{x;s/^/../;x;}; # initialise counter with two tokens /<tag>/,/<\/tag>/ { ...


4

You can use perl -lane 'print "@F[2..$#F] $F[0] $F[1]"' log_file to flip the log lines.


4

With GNU grep linked to a recent version of the PCRE library (Perl Compatible Regular Expressions), you could try: $ grep -oP '<essid\b[^<>]*>\K[^<>]*(?=</essid>)' file WLAN-123651234 This would extract the contents of essid tag. Explanation: <essid matches exactly the string <essid \b called word boundary which matches ...


3

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 | ...


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

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

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

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

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, ...


3

With recent GNU grep built with recent PCRE: grep -Po '<(ELEMENT[12]>)\K.*?(?=</\1)'


3

You really should be using a parser for this, but, just so you know, sed -n '/<tag>/,/<\/tag>/p' file.xml gets you all elements because you print them all. That command works by addressing all lines between a line containing <tag> and the next line in input that contains </tag>. Since that makes pretty much all of your lines, just ...


3

You can do something like: awk '{print} $0 == "<dict>" && previous == "<key>servers</key>" { system("cat other-file.xml") } {previous = $0}'


3

sed '/keys_line_1/,/keys_line_last/{/keys_line_last/{ h;s/unique_split_point.*//;r /path/to/insert/file x;s/.*unique_split_point//;G }}' sed is not exactly forgiving when it comes to requiring adjustments to an hypothesis. Everything sed does is a direct result of the thing it has just done, and so a very minor error in detail can drastically alter ...


3

Why are you using grep to get stuff out of xml-files? You're using grep, so you're probably on a Linux or BSD system, so why not just use xpath from the command line instead? xpath -q -e "SSID/essid/text()" /path/to/file.xml Will do precisely what you want, and with far less hassle than a regular expression which tend to be rather fragile in getting stuff ...


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

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; ...



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