내 파일에는 100줄이 있으며 형식은 다음과 같습니다.
{ "type": "Feature", "properties": { "id":"01","score":"10000","Name": "ABC", "description": "<html xmlns:fo=\"http:\/\/www.w3.org\/1999\/XSL\/Format\" xmlns:msxsl=\"urn:schemas-microsoft-com:xslt\">\n\n<head>\n\n<META http-equiv=\"Content-Type\" content=\"text\/html\">\n\n<meta http-equiv=\"content-type\" content=\"text\/html; charset=UTF-8\">\n\n<\/head>\n\n<body style=\"margin:0px 0px 0px 0px;overflow:auto;background:#FFFFFF;\">\n\n<table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-collapse:collapse;padding:3px 3px 3px 3px\">\n\n<tr style=\"text-align:center;font-weight:bold;background:#9CBCE2\">\n\n<td>PASIR PANJANG 1<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>\n\n<table style=\"font-family:Arial,Verdana,Times;font-size:12px;text-align:left;width:100%;border-spacing:0px; padding:3px 3px 3px 3px\">\n\n<tr>\n\n<td>SHAPE<\/td>\n\n<td>Polygon<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Subzone Number<\/td>\n\n<td>13<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Subzone Name<\/td>\n\n<td>ABC 1<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Subzone Code<\/td>\n\n<td>QTSZ13<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Central Area Indicator<\/td>\n\n<td>N<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Planning Area Name<\/td>\n\n<td>QUEENSTOWN<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Planning Area Code<\/td>\n\n<td>QT<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>Region Name<\/td>\n\n<td>CENTRAL REGION<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Region Code<\/td>\n\n<td>CR<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>INC_CRC<\/td>\n\n<td>1F721290C421BFAB<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>FMEL_UPD_D<\/td>\n\n<td>12\/5\/2014 9:26:20 PM<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>X_ADDR<\/td>\n\n<td>22077.3383<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>Y_ADDR<\/td>\n\n<td>29893.7812<\/td>\n\n<\/tr>\n\n<tr bgcolor=\"#D4E4F3\">\n\n<td>SHAPE_Length<\/td>\n\n<td>6571.323082<\/td>\n\n<\/tr>\n\n<tr>\n\n<td>SHAPE_Area<\/td>\n\n<td>1084792.320587<\/td>\n\n<\/tr>\n\n<\/table>\n\n<\/td>\n\n<\/tr>\n\n<\/table>\n\n<\/body>\n\n<\/html>\n\n"}, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 103.770389070495796, 1.292487855396017 ], [ 103.770414424305599, 1.292574882101212 ], [ 103.7701466034909, 1.29263278185594 ], [ 103.770073030489101, 1.292648696824341 ], [ 103.769991818022206, 1.292739600582147 ] ] ] ] } },
... .. .. 여기에서 각 행을 다음으로 변환해야 합니다.
01,ABC,"POLYGON((103.770389070495796, 1.292487855396017 , 103.770414424305599, 1.292574882101212 , 103.7701466034909, 1.29263278185594 , 103.770073030489101, 1.292648696824341 , 103.769991818022206, 1.292739600582147))"
등.
awk나 sed를 사용하여 이 작업을 수행할 수 있나요?
답변1
JSON을 처리하려면 다음과 같은 JSON 도구를 사용하세요.잭(일반적으로 배포판의 패키지를 사용할 수 있습니다):
jq -j '.properties.id, ",", .([ .geometry.coordinates | recurse | numbers | tostring] | join(",")), "))\n"' < your_file
원하는 한 줄짜리를 수행하십시오. 파일이 실제로 어떻게 보이는지에 따라 각 줄을 개별적으로 입력해야 할 수도 있습니다.
답변2
또 다른 솔루션은jq
:
jq -j '.properties.id, ",",
.properties.Name, ",\"POLYGON((",
(.geometry.coordinates | flatten | map(tostring) | join(",")), "))\"\n"' < file.json
산출:
01,ABC,"POLYGON((103.770389070496,1.29248785539602,103.770414424306,1.29257488210121,103.770146603491,1.29263278185594,103.770073030489,1.29264869682434,103.769991818022,1.29273960058215))"