Wir haben XML-Datei, (Datum:
You are watching: wie man in unix sortiert
wie man XML-Dateien in UNIX sortiert – XML, Sortierung, Unix
Wir haben XML-Datei, (Datum: TT / MM / JJ)
<ABT><pid>101</pid><date>10/12/13</date><name>AAA</name></ABT>
<ABT><pid>102</pid><date>11/12/13</date><name>BBB</name></ABT>
<ABT><pid>101</pid><date>09/12/13</date><name>AAA</name></ABT>
<ABT><pid>102</pid><date>24/12/13</date><name>BBB</name></ABT>
<JRE><pid>101</pid><date>01/12/13</date><name>AAA</name></JRE>
<JRE><pid>102</pid><date>02/12/13</date><name>BBB</name></JRE>
Ausgabe sollte wie sein
<JRE><pid>101</pid><date>01/12/13</date><name>AAA</name></JRE>
<ABT><pid>101</pid><date>09/12/13</date><name>AAA</name></ABT>
<ABT><pid>101</pid><date>10/12/13</date><name>AAA</name></ABT>
<JRE><pid>102</pid><date>02/12/13</date><name>BBB</name></JRE>
<ABT><pid>102</pid><date>11/12/13</date><name>BBB</name></ABT>
<ABT><pid>102</pid><date>24/12/13</date><name>BBB</name></ABT>
So sortieren Sie diese Datei nach <pid>, <date>
.
Antworten:
1 für die Antwort № 1
1 für die Antwort № 1
ich würde … benutzen sort
zusammen mit sed
. Wenn Sie zuerst auf Basis bestellen möchten pid
und dann weiter date
, fügen Sie nach jedem dieser Tags ein Leerzeichen hinzu und sortieren Sie dann entsprechend:
$ sed -e "s/<pid>/& /" -e "s/<date>/& /" file | sort -nk2 -k3 | sed "s/ //g"
<JRE><pid>101</pid><date>01/12/13</date><name>AAA</name></JRE>
<ABT><pid>101</pid><date>09/12/13</date><name>AAA</name></ABT>
<ABT><pid>101</pid><date>10/12/13</date><name>AAA</name></ABT>
<JRE><pid>102</pid><date>02/12/13</date><name>BBB</name></JRE>
<ABT><pid>102</pid><date>11/12/13</date><name>BBB</name></ABT>
<ABT><pid>102</pid><date>24/12/13</date><name>BBB</name></ABT>
Zuerst sed
fügt ein Leerzeichen vor dem Datum ein und das letzte löscht es. Zwischen, sort -n -k2 -k3
sortiert numerisch (-n
), erstens basierend auf Spalte 2 (-k2
) und dann auf Spalte 3 (-k3
).
0 für die Antwort № 2
Sortieren mit xidel
0 für die Antwort № 2
Dieser Befehl wird verwendet xidel um die angegebene Datei zu sortieren file.xml
gemäß pid
und date
.
xidel --xquery "for $i in doc("file.xml")/* order by $i/pid, $i, $i/date return $i" --output-format xml
den Wurzelknoten betrachtend
Du brauchst immer ein Wurzelelement.
Link zum Thema
Fügen Sie also den Wurzelknoten zur Eingabe hinzu:
<root>
<ABT>
<pid>101</pid>
<date>10/12/13</date>
<name>AAA</name>
</ABT>
<ABT>
<pid>102</pid>
<date>11/12/13</date>
<name>BBB</name>
</ABT>
<ABT>
<pid>101</pid>
<date>09/12/13</date>
<name>AAA</name>
</ABT>
<ABT>
<pid>102</pid>
<date>24/12/13</date>
<name>BBB</name>
</ABT>
<JRE>
<pid>101</pid>
<date>01/12/13</date>
<name>AAA</name>
</JRE>
<JRE>
<pid>102</pid>
<date>02/12/13</date>
<name>BBB</name>
</JRE>
</root>
Hinweis: Wie auch immer, xidel kann mit dieser multiplen Root-Struktur umgehen.
Für den Fall, dass alle Knoten, die die direkten Kinder des Wurzelknotens sind, haben <pid>
und <date>
wie Kinder.
Und Ihre XML-Eingabedatei wird benannt file.xml
.
Dieser Befehl gibt die obersten Knoten zurück, nachdem sie nach ihren untergeordneten Namen sortiert wurden <pid>
und <date>
verwenden xidel.
xidel --xquery "<root>
{for $i in doc("file.xml")/*/*
order by $i/pid, $i, $i/date
return $i}
</root>" --output-format xml
Befehl in einer Zeile:
xidel --xquery "<root>{for $i in doc("file.xml")/*/* order by $i/pid, $i, $i/date return $i}</root>" --output-format xml
See more information related to the topic how to sort in unix
Linux sort command summary with examples
- Author: FactorPad
- Post date: 2016-10-17
- Ratings: 4 ⭐ ( 1311 Ratings )
- Match search results: This Linux sort command tutorial shows you to sort lines within text files with examples and syntax. FactorPad Linux Essentials playlist covers your first 100 commands with examples.
Find the code here:
https://factorpad.com/tech/linux-essentials/sort-command.htmlLinux Essentials web page:
https://factorpad.com/tech/linux-essentials/index.htmlhttps://factorpad.com
Don’t lose this valuable resource, subscribe today. Happy Learning!
Let’s connect!
Email lists : http://visitor.r20.constantcontact.com/d.jsp?llr=ewszhmtab&p=oi&m=1120323553988&sit=jxabpaqjb&f=963f605b-ed0a-40a6-be03-00c983ecee37
Facebook: http://facebook.com/factorpad
Twitter: http://twitter.com/factorpad
Pinterest: http://pinterest.com/factorpad
YouTube: http://youtube.com/c/factorpad
LinkedIn: http://linkedin.com/company/factorpad
how to sort xml file in UNIX
- Author: stackoverflow.com
- Ratings: 3 ⭐ ( 2732 Ratings )
- Match search results:
How do I sort a filename in Unix?
- Author: www.compuhoy.com
- Ratings: 3 ⭐ ( 5839 Ratings )
- Match search results: Si está buscando How do I sort a filename in Unix? haga clic aquí. En Compuhoy.com encontrarás todas las respuestas sobre sistemas operativos.
SORT command in Linux/Unix with examples
- Author: www.geeksforgeeks.org
- Ratings: 4 ⭐ ( 8150 Ratings )
- Match search results: A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Sort Command Examples in Unix / Linux Tutorials
- Author: www.folkstalk.com
- Ratings: 4 ⭐ ( 9590 Ratings )
- Match search results: A data warehouse blog contains examples,interview questions and tutorials on Sql,Oracle Plsql,Unix Commands,Linux Commands,Informatica and Netezza.
sort – Unix, Linux Command
- Author: www.tutorialspoint.com
- Ratings: 5 ⭐ ( 7998 Ratings )
- Match search results: sort – Unix, Linux Command,
Write sorted concatenation of all FILE(s) to standard output.
Use the sort command in Unix
- Author: kb.iu.edu
- Ratings: 4 ⭐ ( 5860 Ratings )
- Match search results: The sort command sorts the contents of a file, in numeric
or alphabetic order, and prints the results to standard output
(usually the terminal screen). The …
See more articles in this category: Computer tips