이런 식으로 실행되는 awk 스크립트가 있습니다.
원시 데이터 텍스트:
date:
1.0.1: http://example.com/1.0.1.tgz
1.0.2: http://example.com/1.0.2.tgz
1.0.3: http://example.com/1.0.3.tgz
1.0.4: http://example.com/1.0.4.tgz
1.0.5: http://example.com/1.0.5.tgz
1.0.6: http://example.com/1.0.6.tgz
1.0.7: http://example.com/1.0.7.tgz
1.0.8: http://example.com/1.0.8.tgz
1.0.9: http://example.com/1.0.9.tgz
1.0.10: http://example.com/1.0.10.tgz
awk를 통해 HTML 형식으로 변환:
<table>
<thead>
<tr>
<th>ver</th>
<th>link</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.0.1</td>
<td><a href="http://example.com/1.0.1.tgz">download</a></td>
</tr>
<tr>
<td>1.0.2</td>
<td><a href="http://example.com/1.0.2.tgz">download</a></td>
</tr>
<tr>
<td>1.0.3</td>
<td><a href="http://example.com/1.0.3.tgz">download</a></td>
</tr>
<tr>
<td>1.0.4</td>
<td><a href="http://example.com/1.0.4.tgz">download</a></td>
</tr>
<tr>
<td>1.0.5</td>
<td><a href="http://example.com/1.0.5.tgz">download</a></td>
</tr>
<tr>
<td>1.0.6</td>
<td><a href="http://example.com/1.0.6.tgz">download</a></td>
</tr>
<tr>
<td>1.0.7</td>
<td><a href="http://example.com/1.0.7.tgz">download</a></td>
</tr>
<tr>
<td>1.0.8</td>
<td><a href="http://example.com/1.0.8.tgz">download</a></td>
</tr>
<tr>
<td>1.0.9</td>
<td><a href="http://example.com/1.0.9.tgz">download</a></td>
</tr>
<tr>
<td>1.0.10</td>
<td><a href="http://example.com/1.0.10.tgz">download</a></td>
</tr>
</tbody>
</table>
양식의 "다운로드" 텍스트를 링크된 파일 이름으로 바꾸고 싶습니다. 어떻게 수정해야 하나요? 아래는 기존 awk 스크립트 코드입니다.
#!/usr/bin/env awk
BEGIN {
print "<table>"
print "\t<thead>"
print "\t\t<tr>"
print "\t\t\t<th>ver</th>"
print "\t\t\t<th>link</th>"
print "\t\t</tr>"
print "\t</thead>"
print "\t<tbody>"
}
match($0, /^ +(.*): (.*)$/, r) {
print "\t\t<tr>"
printf "\t\t\t<td>%s</td>\n", r[1]
printf "\t\t\t<td><a href=\"%s\">download</a></td>\n", r[2]
print "\t\t</tr>"
}
END {
print "\t</tbody>"
print "</table>"
}
나는 초보자이며 당신이 나를 도울 수 있기를 바랍니다. 도움이 되는 제안이 있으면 미리 감사드립니다!
답변1
18번째 줄에서 이것을 시도해 보세요.
printf "\t\t\t<td><a href=\"%s\">%s.tgz</a></td>\n", r[2], r[1]
답변2
match() 정규식에서 세 번째 캡처 그룹을 만들어 파일 이름을 보관하고 적절한 줄에 인쇄하면 됩니다.
$ cat tst.awk
BEGIN {
print "<table>"
print "\t<thead>"
print "\t\t<tr>"
print "\t\t\t<th>ver</th>"
print "\t\t\t<th>link</th>"
print "\t\t</tr>"
print "\t</thead>"
print "\t<tbody>"
}
match($0, /^ +(.*): (.*\/([^/]+))$/, r) {
print "\t\t<tr>"
printf "\t\t\t<td>%s</td>\n", r[1]
printf "\t\t\t<td><a href=\"%s\">%s</a></td>\n", r[2], r[3]
print "\t\t</tr>"
}
END {
print "\t</tbody>"
print "</table>"
}
.
$ awk -f tst.awk data.text
<table>
<thead>
<tr>
<th>ver</th>
<th>link</th>
</tr>
</thead>
<tbody>
<tr>
<td>1.0.1</td>
<td><a href="http://example.com/1.0.1.tgz">1.0.1.tgz</a></td>
</tr>
<tr>
<td>1.0.2</td>
<td><a href="http://example.com/1.0.2.tgz">1.0.2.tgz</a></td>
</tr>
<tr>
<td>1.0.3</td>
<td><a href="http://example.com/1.0.3.tgz">1.0.3.tgz</a></td>
</tr>
<tr>
<td>1.0.4</td>
<td><a href="http://example.com/1.0.4.tgz">1.0.4.tgz</a></td>
</tr>
<tr>
<td>1.0.5</td>
<td><a href="http://example.com/1.0.5.tgz">1.0.5.tgz</a></td>
</tr>
<tr>
<td>1.0.6</td>
<td><a href="http://example.com/1.0.6.tgz">1.0.6.tgz</a></td>
</tr>
<tr>
<td>1.0.7</td>
<td><a href="http://example.com/1.0.7.tgz">1.0.7.tgz</a></td>
</tr>
<tr>
<td>1.0.8</td>
<td><a href="http://example.com/1.0.8.tgz">1.0.8.tgz</a></td>
</tr>
<tr>
<td>1.0.9</td>
<td><a href="http://example.com/1.0.9.tgz">1.0.9.tgz</a></td>
</tr>
<tr>
<td>1.0.10</td>
<td><a href="http://example.com/1.0.10.tgz">1.0.10.tgz</a></td>
</tr>
</tbody>
</table>