Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

linux - 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

Thanks in advanced!


I also asked similar question here with output example and desired output: xpath html combine columns

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With xmlstarlet and awk:

wget -q -O - "http://www.example.com" | xmlstarlet sel -t -v "//tr/td" -n 
| awk -F'
' -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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...