对文件中每一行的文本重新排序,并最终转换为 XML

对文件中每一行的文本重新排序,并最终转换为 XML

我正在处理一个日志文件,我需要对每一行重新排序(而不是排序)。日志文件如下所示:

11-06-2014 - 10:49:06PM lat = 41.858657; lon = -91.345142
11-06-2014 - 10:49:49PM lat = 42.864653; lon = -92.349914
11-06-2014 - 10:50:35PM lat = 43.874808; lon = -93.350364
11-06-2014 - 10:51:21PM lat = 44.885047; lon = -94.350058
11-06-2014 - 10:52:08PM lat = 45.895078; lon = -95.349920
11-06-2014 - 10:53:30PM lat = 46.905178; lon = -96.349837
11-06-2014 - 10:54:50PM lat = 47.910303; lon = -97.350606

我想将日期/时间移到每行的末尾:

lat = 41.858657; lon = -91.345142 11-06-2014 - 10:49:06PM
lat = 42.864653; lon = -92.349914 11-06-2014 - 10:49:49PM
lat = 43.874808; lon = -93.350364 11-06-2014 - 10:50:35PM
lat = 44.885047; lon = -94.350058 11-06-2014 - 10:51:21PM
lat = 45.895078; lon = -95.349920 11-06-2014 - 10:52:08PM
lat = 46.905178; lon = -96.349837 11-06-2014 - 10:53:30PM
lat = 47.910303; lon = -97.350606 11-06-2014 - 10:54:50PM

sedawk?如何?

额外问题:我的最终目标是将其转换为 gpx/xml,并且在处理每行时添加中间文本可能同样容易,因此输出如下所示:

<wpt lat="41.858657" lon="-91.345142">
    <time>11-06-2014 - 10:49:06PM</time>
</wpt>

<wpt lat"="42.864653" lon="-92.349914">
     <time>11-06-2014 - 10:49:49PM</time>
</wpt>

<wpt lat"="43.874808" lon="-93.350364">
     <time>11-06-2014 - 10:50:35PM</time>
</wpt>

<wpt lat"="44.885047" lon="-94.350058">
     <time>11-06-2014 - 10:51:21PM</time>
</wpt>

<wpt lat"="45.895078" lon="-95.349920">
     <time>11-06-2014 - 10:52:08PM</time>
</wpt>

<wpt lat"="46.905178" lon="-96.349837">
     <time>11-06-2014 - 10:53:30PM</time>
</wpt>

<wpt lat"="47.910303" lon="-97.350606">
     <time>11-06-2014 - 10:54:50PM</time>
</wpt>

答案1

使用 awk :

 awk '{print $4,$5,$6,$7,$8,$9,$1,$2,$3}' log_file

您可以直接从日志文件中执行此操作,如下所示:

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

输出:

<wpt lat="41.858657" lon="-91.345142">
<time>11-06-2014 - 10:49:06PM</time>
</wpt>
<wpt lat="42.864653" lon="-92.349914">
<time>11-06-2014 - 10:49:49PM</time>
</wpt>
<wpt lat="43.874808" lon="-93.350364">
<time>11-06-2014 - 10:50:35PM</time>
</wpt>
<wpt lat="44.885047" lon="-94.350058">
<time>11-06-2014 - 10:51:21PM</time>
</wpt>
<wpt lat="45.895078" lon="-95.349920">
<time>11-06-2014 - 10:52:08PM</time>
</wpt>
<wpt lat="46.905178" lon="-96.349837">
<time>11-06-2014 - 10:53:30PM</time>
</wpt>
<wpt lat="47.910303" lon="-97.350606">
<time>11-06-2014 - 10:54:50PM</time>
</wpt>

答案2

您可以使用

perl -lane 'print "@F[2..$#F] $F[0] $F[1]"' log_file

翻转日志行。

答案3

sed '   s/ *[=;]"* */="/g;s//" /2         #first sub-out spaces; for "
        s|\(.*M\) \(.*\)|\                #then do the replace
<wpt \2">\
        <time>\1</time>\
</wpt>|
' <<\INPUT
11-06-2014 - 10:49:06PM lat = 41.858657; lon = -91.345142
11-06-2014 - 10:49:49PM lat = 42.864653; lon = -92.349914
11-06-2014 - 10:50:35PM lat = 43.874808; lon = -93.350364
11-06-2014 - 10:51:21PM lat = 44.885047; lon = -94.350058
11-06-2014 - 10:52:08PM lat = 45.895078; lon = -95.349920
11-06-2014 - 10:53:30PM lat = 46.905178; lon = -96.349837
11-06-2014 - 10:54:50PM lat = 47.910303; lon = -97.350606
INPUT

这就是我想出来的。在 中这相当简单sed。我在这里使用文字字符,因为它对我来说更容易阅读,而且并非所有sed's 都会处理右侧替换字段中的反斜杠转义。如果你是这样,那就随意吧。无论如何,这些内联注释肯定会弄乱任何内容sed,因此请在测试之前删除它们。

输出

<wpt lat="41.858657" lon="-91.345142">
        <time>11-06-2014 - 10:49:06PM</time>
</wpt>

<wpt lat="42.864653" lon="-92.349914">
        <time>11-06-2014 - 10:49:49PM</time>
</wpt>

<wpt lat="43.874808" lon="-93.350364">
        <time>11-06-2014 - 10:50:35PM</time>
</wpt>

<wpt lat="44.885047" lon="-94.350058">
        <time>11-06-2014 - 10:51:21PM</time>
</wpt>

<wpt lat="45.895078" lon="-95.349920">
        <time>11-06-2014 - 10:52:08PM</time>
</wpt>

<wpt lat="46.905178" lon="-96.349837">
        <time>11-06-2014 - 10:53:30PM</time>
</wpt>

<wpt lat="47.910303" lon="-97.350606">
        <time>11-06-2014 - 10:54:50PM</time>
</wpt>

答案4

同样适用于sed

sed -E 's&(.*M)( .*);( .*)$&<wpt\2\3>\n\t<time>\1</time>\n</wpt>&;
        $!G;
        s& = ([-.0-9]+)&="\1"&g' log.file

或者将其放入脚本文件中

#!/bin/sed -Ef
s|(.*M)( .*);( .*)$|<wpt\2\3>\n\t<time>\1</time>\n</wpt>|
$!G
s| = ([-.0-9]+)|="\1"|g

并使用它

sed -Ef script.file log.file

chmod +x ./script.file或者如果通过以下方式使其可执行

./script.file log.file

相关内容