[投訴]
[投訴關閉]
但是你提出我的好奇心。因此,我試圖解決它。 (特別是提的示例數據Stolperstein
–很聰明現在,我感覺像一個「使命是幫助好人」 ......)
我簡化你的問題有點:
我假設領域id
,lon
和lat
是強制性的。
我認爲可選的命名字段爲name
,historic
和image
。
我的測試數據test-complete-lines.txt
:
n3329319394;4.369872;50.866430;name=Klaus Mustermann;historic=memorial;image=j.doe-de.png
n3329319395;4.369872;50.866430;name=Gabi Mustermann
n4208925477;5.041860;52.141352;historic=memorial
n4208925477;5.041860;52.141352;image=the-image.png
n3329319395;4.369872;50.866430;name=Gabi Mustermann;historic=memorial
n3329319395;4.369872;50.866430;name=Gabi Mustermann;image=j.doe.female-de.png
我的腳本test-complete-lines.awk
:
BEGIN { FS=";" }
# get mandatory fields id, lon, lat
{ id = $1 ; lon = $2 ; lat = $3 }
# set optional fields empty
{ name=";name=" ; historic=";historic=" ; image=";image=" }
# replace found fields with values
/;name=/ { name = gensub(/^.*(;name=[^;]*).*$/, "\\1", "g", $0) }
/;historic=/ { historic = gensub(/^.*(;historic=[^;]*).*$/, "\\1", "g", $0) }
/;image=/ { image = gensub(/^.*(;image=[^;]*).*$/, "\\1", "g", $0) }
# print processed line
{ print id";"lon";"lat""name""historic""image }
測試與GAWK(bash中,Cygwin的時,Windows 10(64位)):
$ awk --version
GNU Awk 4.1.4, API: 1.1 (GNU MPFR 3.1.5-p10, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2016 Free Software Foundation.
$ awk -f test-complete-lines.awk <test-complete-lines.txt
n3329319394;4.369872;50.866430;name=Klaus Mustermann;historic=memorial;image=j.doe-de.png
n3329319395;4.369872;50.866430;name=Gabi Mustermann;historic=;image=
n4208925477;5.041860;52.141352;name=;historic=memorial;image=
n4208925477;5.041860;52.141352;name=;historic=;image=the-image.png
n3329319395;4.369872;50.866430;name=Gabi Mustermann;historic=memorial;image=
n3329319395;4.369872;50.866430;name=Gabi Mustermann;historic=;image=j.doe.female-de.png
$
備註:
替換找到的字段假定沒有;
將出現在內容中。我建議你做一個計數器樣本(其中;
出現在內容中)。這可能會激活某種引用或轉義。因此,可能需要額外處理這種情況。
我只提到了一些命名的字段。您必須在計劃後添加其餘部分。
Btw。我的示例文本中意外地出現了一行空行。這產生:
;;;name=;historic=;image=
如果需要處理空行,另一個規則可能會(後BEGIN { }
)插入:
/^[ \t]*$/ { skip }
在我的第一個版本,我在樣本數據–一個被遺忘的;
一個錯字。因此,image=
成爲name=
的內容,但也被確認爲個別領域。假設命名字段可能不會作爲第一個字段出現,我將其包括前面的;
修正爲字段名稱的模式。