'xpath html get all columns 1 and 2 together and concatenate with column ":"
I have this following command that gets the data from column 2:
Table example:
<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<td>d</td>
<td>e</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
wget -q -O - http://www.example.com | xmllint --html --xpath "//table[@id=\"tableID\"]//tr//td[position() = 2]//text() - 2>/dev/null
That outputs something like:
12345
How can I get all both column 1 and column 2 with ":" symbol that appends on each line?
Desired output:
a:1
b:2
c:3
d:4
e:5
Solution 1:[1]
With xmlstarlet and awk:
wget -q -O - "http://www.example.com" | xmlstarlet sel -t -v "//tr/td" -n \
| awk -F'\n' -v RS= '{ n=NF/2; for(i=1;i<=n;i++) print $i ":" $(i+n) }'
The output:
a:1
b:2
c:3
d:4
e:5
Solution 2:[2]
With xidel, which, unlike xmllint and xmlstarlet, supports XPath/XQuery 3.1:
$ xidel -s "<file-or-url>" -e '
for $col in 1 to count(//tr[1]/td) return
join(
for $row in 1 to count(//tr) return
//tr[$row]/td[$col],
":"
)
'
a:1
b:2
c:3
d:4
e:5
Solution 3:[3]
resolved from: xpath html combine columns
solution:
wget -q -O - "https://socks-proxy.net" \
| xmllint --html --xpath "//table[@id='proxylisttable']//tr//td[position() < 3]" - 2>/dev/null
| tidy -cq -omit -f /dev/null | xmllint --html --xpath "//td/text()" - | paste - - -d':'
many thanks to @RomanPerekhrest
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RomanPerekhrest |
| Solution 2 | Reino |
| Solution 3 | John Mark |
