Text-Processing
對文件中每一行的文本重新排序,並最終轉換為 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
sed
?awk
? 如何?額外的問題:我的最終目標是將其轉換為 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>
使用 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>