Tag Info

New answers tagged

0

To answer this question properly, we'd ideally need a better example - some valid xml is a good start. Also - an example of desired output. You don't, for example, indicate where you'd want the <C> and <D> elements to end up within your resultant XML. They're already children of <B> - do you want to preserve B or reparent C and D to the ...


0

Please don't use awk sed etc. They cannot handle XML properly. XML does a bunch of stuff like having whitespace, linefeeds, unary tags etc. that means regular expressions aren't very robust - they break messily, following a perfectly valid change to XML down the line. The way to handle XML is with a parser. xmlstarlet is one commonly used on Linux. Because ...


1

The following awk script (plus column for output tabulation) will hande any sequence of placement of the sub-tags, and any whitespace separation of the tags - ie. it will handle the OP's sample input format, as well as the following sample which has no whitespace and differently ordered sub-tags: <HARDWARE><OS>Windows ...


0

with awk - arbitrarily set each column to be 15 characters long, left-aligned and filled with spaces: awk ' BEGIN { FS = "<[A-Za-z/]+>" } { if ( NR % 6 == 0 ) { printf"\n" } else if ( $2 != "" ) { printf"%-15s", $2 } }' file Or as in the other answers in combination with column awk ' BEGIN { FS = "<[A-Za-z/]+>" } { if ( NR % 6 == 0 ) { ...


6

With a slight modification to your XML, wrap all your XML in a parent <DATA> tag1, or another one of your choosing, file called data.xml: <DATA> <HARDWARE> <NAME>WIN1</NAME> <OS>Windows 7</OS> <IP>1.2.3.4</IP> <DOMAIN>contoso.com</DOMAIN> </HARDWARE> <HARDWARE> ...


2

With your example and GNU sed: sed -n 's/<[^>]*>//g;s/^ *//g;/./p' file | paste -d ";" - - - - | column -t -s ";" Output: WIN1 Windows 7 1.2.3.4 contoso.com WIN2 Windows 8 10.20.30.40 contoso.com I assume that your file does not contain a ;. If you need a CSV remove | column -t -s ";".


2

The xmlstarlet tool will do this: xmlstarlet sel -t -m /A -o ID, -v id -n -o C, -v //C -n -o D, -v //D -n test.xml For each A under the root element (-m /A), it prints the string "ID," (-o ID,), the contents of id (-v id), a newline (-n), and likewise for children C (-v //C)and D (-v //D) with their respective headers. The double slashes are the XPath ...


0

Late answer here: Ubuntu repository has a very good utility called xmlto that could help you. It converts xml to a variety of formats, including plain text, epub, pdf. Online, there is Oxgarage which has many conversion options.


0

Please - don't use sed - it's not a suitable tool for the job. I'd use perl myself: #!/usr/bin/env perl use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new( 'pretty_print' => 'indented_a' ); $twig->parsefile ( 'your_file.xml' ); foreach my $thing ( $twig -> root -> children ) { my $newthing = $twig -> root -> ...


2

process.xsl: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="//book"> <xsl:element name="book"> <xsl:apply-templates select="./@*"/> </xsl:element> ...


1

With awk you can do something like this: Supposing the line is in file file1.xml then: awk -F '/' '{print $1,"Book_Width=\"A\"",FS,$2}' file1.xml


3

in sed "a" appends a pattern IN A NEW LINE. what you want to do is replace (substitute). Let's use a colon as separator for clarity: sed 's:\(<book.*\)\(/>\):\1 Book_Width="A"\2:' anything in \( .. \) is a pattern memorized by the order of appearance and recalled by \indexnumber , e.g. \1 will reproduce the first pattern saved. So we are ...


10

You should not parse xml with sed, use an xml parser like xmlstarlet instead. For your task it would be: xmlstarlet ed -O --inplace --insert "/book" --type attr -n Book_Width -v A xml_file The file content is then: <book name="Sed tutorial" price="250" Book_Width="A"/> The ed means edit mode to edit the xml tree -O omits the xml tag We want to ...


1

Please, on behalf of future maintenance programmers and sysadmins - DON'T use a regex to parse XML. XML is a structured data type, and it is NOT well suited for regex parsing - you can 'fake it' by pretending it's plain text, but there's a bunch of semantically identical things in XML that don't parse the same. You can embed linefeeds, and have unary tags ...


0

Command to search the exact expression in Korn Shell of Unix grep -w "SEARCH_EXPRESSION" FILE_TO_BE_SEARCHED The above command searches for the exact word or search expression.


3

With the -F option, grep looks for exact matches (regex features turned off): $ grep -F "defabc" xyz.txt defabc grep sets an appropriate return code so that we can test for true or false: $ if grep -qF "defabc" xyz.txt; then echo True; else echo False; fi True $ if grep -qF "Defabc" xyz.txt; then echo True; else echo False; fi False Because we only ...



Top 50 recent answers are included