用bbcode创建一个格式化字符串



我有一个HTML表,里面有一些关于部落的玩家信息。我的目标是将这个HTML表转换为一些bbcode。

例如,源HTML如下:http://www.wa-sta.net/index.php?menu=clan&clanid=1091&get=成员

bbcode应将表格格式化为:

    #      Position        Nickname        Region  Level   Kills   Deaths  KD/R
    1      Leader          Syrocs          Germany 56      76,307  58,677  1.3
    2      Manager         Syroxs          Germany 55      69,472  64,729  1.073
    3      Manager         checker284      Germany 43      43,827  62,385  0.703
    4      Member          Jeynox          Germany 77      206,387 185,675 1.112
    ...

所需代码如下:

    [table]
    [tr][td][b]#[/b][/td][td][td][b]Position[/b][/td][td][b]Nickname[/b][/td][td][b]Region[/b][/td][td][b]Level[/b][/td][td][b]Kills[/b][/td][td][b]Deaths[/b][/td][td][b]KD/R[/b][/td][/tr]
    [tr][td]1[/td][td]Leader Syrocs[/td][td]Germany[/td][td]56[/td][td]76,307[/td][td]58,677[/td][td]1.3[/td][/tr]
    .
    .
    .
    [/table]

我的脚本可以正确地获取信息,但我的问题是,我不知道如何用整个bbcode创建一个字符串。。。

脚本实际有以下代码:

    #!/bin/bash
    # Fetch current level of client from WA-STA.NET
    WASTA_USER_URL="http://www.wa-sta.net/index.php?menu=clan&clanid=1091&get=members"
    # Create temp files
    TEMPFILE_1=$(mktemp /tmp/wasta_download_page-XXXXX)
    TEMPFILE_2=$(mktemp /tmp/wasta_download_page-XXXXX)
    TEMPFILE_3=$(mktemp /tmp/wasta_download_page-XXXXX)
    TEMPFILE_4=$(mktemp /tmp/wasta_download_page-XXXXX)
    # Download 'WA-STA.com Clanmember List'
    wget $WASTA_USER_URL -q -O - > $TEMPFILE_1
    # Get the clanlist
    sed -n "/<tbody>/,/</table>/p" $TEMPFILE_1 > $TEMPFILE_2
    cat $TEMPFILE_2 | tr "n" "|" | grep -o '<tr>.*</tr>' | sed 's/(<tr>|</tr>)//g' | sed 's/|/n/g' > $TEMPFILE_3
    ROW=1
    while read line; do
            if [ $ROW -eq 1 ]; then
                    # ID
                    echo "$line" | egrep -o '<td>[0-9]+</td>' | egrep -o '[0-9]+'
            elif [ $ROW -eq 2 ]; then
                    # Position
                    echo "$line" | egrep -o 'Leader|Manager|Member'
            elif [ $ROW -eq 3 ]; then
                    # Nickname
                    echo "$line" | egrep -o '[a-zA-Z0-9]+</' | egrep -o '[a-zA-Z0-9]+'
            elif [ $ROW -eq 4 ]; then
                    # Region
                    echo "$line" | egrep -o '/img/flags/[a-zA-Z]+-Flag-48.png'
            elif [ $ROW -eq 5 ]; then
                    # Level
                    echo "$line" | egrep -o 'iLevel-LevelIcon_L[0-9]+' | egrep -o '[0-9]+'
            elif [ $ROW -eq 6 ]; then
                    # Kills
                    echo "$line" | egrep -o '[0-9,?]+'
            elif [ $ROW -eq 7 ]; then
                    # Deaths
                    echo "$line" | egrep -o '[0-9,?]+'
            elif [ $ROW -eq 8 ]; then
                    echo "$line" | egrep -o '[0-9.?]+'
            fi
            if [[ $? -eq 0 ]]; then
                    let "ROW = $ROW + 1"
                    if [ $ROW -eq 9 ]; then
                            ROW=1
                            echo "-------------------------------------------"
                    fi
            fi
    done < $TEMPFILE_3 > $TEMPFILE_4
    cat $TEMPFILE_4
    rm $TEMPFILE_1
    rm $TEMPFILE_2
    rm $TEMPFILE_3
    rm $TEMPFILE_4
    exit 0;

并返回以下内容,如果我执行它:

    $ ./script.sh
    1
    Leader
    Syrocs
    /img/flags/Germany-Flag-48.png
    56
    76,307
    58,677
    1.3
    -------------------------------------------
    2
    Manager
    Syroxs
    /img/flags/Germany-Flag-48.png
    55
    69,472
    64,729
    1.073
    -------------------------------------------
    3
    Manager
    checker284
    /img/flags/Germany-Flag-48.png
    44
    43,827
    62,385
    0.703
    -------------------------------------------
    4
    Member
    Jeynox
    /img/flags/Germany-Flag-48.png
    73
    206,387
    185,675
    1.112
    -------------------------------------------
    5
    Member
    0xtime2rushx0
    /img/flags/Germany-Flag-48.png
    64
    1,767
    1,003
    1.762
    -------------------------------------------
    6
    Member
    FireFox7x
    /img/flags/Germany-Flag-48.png
    59
    60,913
    42,815
    1.423
    -------------------------------------------
    7
    Member
    xxl3aaamxx
    /img/flags/Germany-Flag-48.png
    52
    29,408
    25,937
    1.134
    -------------------------------------------
    8
    Member
    moechtegern
    /img/flags/Germany-Flag-48.png
    46
    34,986
    36,034
    0.971
    -------------------------------------------
    9
    Member
    Syroxz
    /img/flags/Germany-Flag-48.png
    45
    16,371
    21,166
    0.773
    -------------------------------------------
    10
    Member
    xFameB0zZ
    /img/flags/Germany-Flag-48.png
    43
    38,007
    31,258
    1.216
    -------------------------------------------
    11
    Member
    xDirtyCrash7x
    /img/flags/Germany-Flag-48.png
    40
    37,959
    24,787
    1.531
    -------------------------------------------
    12
    Member
    michii1992
    /img/flags/Austria-Flag-48.png
    35
    27,993
    18,076
    1.549
    -------------------------------------------
    13
    Member
    GymBeast
    /img/flags/Germany-Flag-48.png
    35
    14,220
    11,958
    1.189
    -------------------------------------------
    14
    Member
    vanillaz55
    /img/flags/Germany-Flag-48.png
    35
    24,575
    18,936
    1.298
    -------------------------------------------
    15
    Member
    xNiino
    /img/flags/Germany-Flag-48.png
    34
    15,093
    11,157
    1.353
    -------------------------------------------
    16
    Member
    Lowglette88
    /img/flags/Germany-Flag-48.png
    30
    1,472
    1,473
    0.999
    -------------------------------------------
    17
    Member
    stumpf7
    /img/flags/Germany-Flag-48.png
    29
    18,463
    19,397
    0.952
    -------------------------------------------
    18
    Member
    xChAoSxSKiLLzzx
    /img/flags/Switzerland-Flag-48.png
    27
    15,545
    13,434
    1.157
    -------------------------------------------
    19
    Member
    xeasybeatzzx
    /img/flags/Germany-Flag-48.png
    25
    7,782
    9,709
    0.802
    -------------------------------------------
    20
    Member
    utzmaster1
    /img/flags/Germany-Flag-48.png
    19
    8,202
    12,065
    0.68
    -------------------------------------------
    21
    Member
    Jaynox
    /img/flags/Germany-Flag-48.png
    19
    1,711
    2,065
    0.829
    -------------------------------------------
    22
    Member
    ETOJohnson
    /img/flags/Germany-Flag-48.png
    17
    3,707
    3,904
    0.95
    -------------------------------------------
    23
    Member
    xphoenix7
    /img/flags/Germany-Flag-48.png
    16
    3,930
    3,067
    1.281
    -------------------------------------------
    24
    Member
    Vegetox
    /img/flags/Germany-Flag-48.png
    13
    824
    3,224
    0.256
    -------------------------------------------
    25
    Member
    Walr0ssMAnn
    /img/flags/Germany-Flag-48.png
    11
    1,851
    1,701
    1.088
    -------------------------------------------
    26
    Member
    0xtime2f4mex0
    /img/flags/Germany-Flag-48.png
    2
    503
    310
    1.623
    -------------------------------------------
    27
    Member
    Raiinbow7x
    /img/flags/Germany-Flag-48.png
    2
    93
    31
    3
    -------------------------------------------
    28
    Member
    xL1MiTx
    /img/flags/Germany-Flag-48.png
    1
    3
    4
    0.75
    -------------------------------------------

我希望有人能帮助我。:/

将脚本的输出作为输入

awk 'BEGIN {print "[table]";ORS=""; print "[tr][td][b]#[/b][/td][td][td][b]Position[/b][/td][td][b]Nickname[/b][/td][td][b]Region[/b][/td][td][b]Level[/b][/td][td][b]Kills[/b][/td][td][b]Deaths[/b][/td][td][b]KD/R[/b][/td][/tr]"} {for(i=0;i<8;i++){gsub(/[[:space:]]$|^[ t]+|/img/flags/|-Flag-48.png/,"",$0);arr[i]="[td]"$0"[/td]";getline;} print "n[tr]"; for(i in arr) print arr[i]; print "[/tr]";} END{print "n[/table]";}' input.txt

输入:

1
Leader
Syrocs
/img/flags/Germany-Flag-48.png
56
76,307
58,677
1.3
-------------------------------------------
2
Manager
Syroxs
/img/flags/Germany-Flag-48.png
55
69,472
64,729
1.073
-------------------------------------------
3
Manager
checker284
/img/flags/Germany-Flag-48.png
44
43,827
62,385
0.703
-------------------------------------------
4
Member
Jeynox
/img/flags/Germany-Flag-48.png
73
206,387
185,675
1.112
-------------------------------------------
5
Member
0xtime2rushx0
/img/flags/Germany-Flag-48.png
64
1,767
1,003
1.762
-------------------------------------------
6
Member
FireFox7x
/img/flags/Germany-Flag-48.png
59
60,913
42,815
1.423
-------------------------------------------
7
Member
xxl3aaamxx
/img/flags/Germany-Flag-48.png
52
29,408
25,937
1.134
-------------------------------------------
8
Member
moechtegern
/img/flags/Germany-Flag-48.png
46
34,986
36,034
0.971
-------------------------------------------
9
Member
Syroxz
/img/flags/Germany-Flag-48.png
45
16,371
21,166
0.773
-------------------------------------------
10
Member
xFameB0zZ
/img/flags/Germany-Flag-48.png
43
38,007
31,258
1.216
-------------------------------------------
11
Member
xDirtyCrash7x
/img/flags/Germany-Flag-48.png
40
37,959
24,787
1.531
-------------------------------------------
12
Member
michii1992
/img/flags/Austria-Flag-48.png
35
27,993
18,076
1.549
-------------------------------------------
13
Member
GymBeast
/img/flags/Germany-Flag-48.png
35
14,220
11,958
1.189
-------------------------------------------
14
Member
vanillaz55
/img/flags/Germany-Flag-48.png
35
24,575
18,936
1.298
-------------------------------------------
15
Member
xNiino
/img/flags/Germany-Flag-48.png
34
15,093
11,157
1.353
-------------------------------------------
16
Member
Lowglette88
/img/flags/Germany-Flag-48.png
30
1,472
1,473
0.999
-------------------------------------------
17
Member
stumpf7
/img/flags/Germany-Flag-48.png
29
18,463
19,397
0.952
-------------------------------------------
18
Member
xChAoSxSKiLLzzx
/img/flags/Switzerland-Flag-48.png
27
15,545
13,434
1.157
-------------------------------------------
19
Member
xeasybeatzzx
/img/flags/Germany-Flag-48.png
25
7,782
9,709
0.802
-------------------------------------------
20
Member
utzmaster1
/img/flags/Germany-Flag-48.png
19
8,202
12,065
0.68
-------------------------------------------
21
Member
Jaynox
/img/flags/Germany-Flag-48.png
19
1,711
2,065
0.829
-------------------------------------------
22
Member
ETOJohnson
/img/flags/Germany-Flag-48.png
17
3,707
3,904
0.95
-------------------------------------------
23
Member
xphoenix7
/img/flags/Germany-Flag-48.png
16
3,930
3,067
1.281
-------------------------------------------
24
Member
Vegetox
/img/flags/Germany-Flag-48.png
13
824
3,224
0.256
-------------------------------------------
25
Member
Walr0ssMAnn
/img/flags/Germany-Flag-48.png
11
1,851
1,701
1.088
-------------------------------------------
26
Member
0xtime2f4mex0
/img/flags/Germany-Flag-48.png
2
503
310
1.623
-------------------------------------------
27
Member
Raiinbow7x
/img/flags/Germany-Flag-48.png
2
93
31
3
-------------------------------------------
28
Member
xL1MiTx
/img/flags/Germany-Flag-48.png
1
3
4
0.75
-------------------------------------------

输出:

[table]
[tr][td][b]#[/b][/td][td][td][b]Position[/b][/td][td][b]Nickname[/b][/td][td][b]Region[/b][/td][td][b]Level[/b][/td][td][b]Kills[/b][/td][td][b]Deaths[/b][/td][td][b]KD/R[/b][/td][/tr]
[tr][td]1[/td][td]Leader[/td][td]Syrocs[/td][td]Germany[/td][td]56[/td][td]76,307[/td][td]58,677[/td][td]1.3[/td][/tr]
[tr][td]2[/td][td]Manager[/td][td]Syroxs[/td][td]Germany[/td][td]55[/td][td]69,472[/td][td]64,729[/td][td]1.073[/td][/tr]
[tr][td]3[/td][td]Manager[/td][td]checker284[/td][td]Germany[/td][td]44[/td][td]43,827[/td][td]62,385[/td][td]0.703[/td][/tr]
[tr][td]4[/td][td]Member[/td][td]Jeynox[/td][td]Germany[/td][td]73[/td][td]206,387[/td][td]185,675[/td][td]1.112[/td][/tr]
[tr][td]5[/td][td]Member[/td][td]0xtime2rushx0[/td][td]Germany[/td][td]64[/td][td]1,767[/td][td]1,003[/td][td]1.762[/td][/tr]
[tr][td]6[/td][td]Member[/td][td]FireFox7x[/td][td]Germany[/td][td]59[/td][td]60,913[/td][td]42,815[/td][td]1.423[/td][/tr]
[tr][td]7[/td][td]Member[/td][td]xxl3aaamxx[/td][td]Germany[/td][td]52[/td][td]29,408[/td][td]25,937[/td][td]1.134[/td][/tr]
[tr][td]8[/td][td]Member[/td][td]moechtegern[/td][td]Germany[/td][td]46[/td][td]34,986[/td][td]36,034[/td][td]0.971[/td][/tr]
[tr][td]9[/td][td]Member[/td][td]Syroxz[/td][td]Germany[/td][td]45[/td][td]16,371[/td][td]21,166[/td][td]0.773[/td][/tr]
[tr][td]10[/td][td]Member[/td][td]xFameB0zZ[/td][td]Germany[/td][td]43[/td][td]38,007[/td][td]31,258[/td][td]1.216[/td][/tr]
[tr][td]11[/td][td]Member[/td][td]xDirtyCrash7x[/td][td]Germany[/td][td]40[/td][td]37,959[/td][td]24,787[/td][td]1.531[/td][/tr]
[tr][td]12[/td][td]Member[/td][td]michii1992[/td][td]Austria[/td][td]35[/td][td]27,993[/td][td]18,076[/td][td]1.549[/td][/tr]
[tr][td]13[/td][td]Member[/td][td]GymBeast[/td][td]Germany[/td][td]35[/td][td]14,220[/td][td]11,958[/td][td]1.189[/td][/tr]
[tr][td]14[/td][td]Member[/td][td]vanillaz55[/td][td]Germany[/td][td]35[/td][td]24,575[/td][td]18,936[/td][td]1.298[/td][/tr]
[tr][td]15[/td][td]Member[/td][td]xNiino[/td][td]Germany[/td][td]34[/td][td]15,093[/td][td]11,157[/td][td]1.353[/td][/tr]
[tr][td]16[/td][td]Member[/td][td]Lowglette88[/td][td]Germany[/td][td]30[/td][td]1,472[/td][td]1,473[/td][td]0.999[/td][/tr]
[tr][td]17[/td][td]Member[/td][td]stumpf7[/td][td]Germany[/td][td]29[/td][td]18,463[/td][td]19,397[/td][td]0.952[/td][/tr]
[tr][td]18[/td][td]Member[/td][td]xChAoSxSKiLLzzx[/td][td]Switzerland[/td][td]27[/td][td]15,545[/td][td]13,434[/td][td]1.157[/td][/tr]
[tr][td]19[/td][td]Member[/td][td]xeasybeatzzx[/td][td]Germany[/td][td]25[/td][td]7,782[/td][td]9,709[/td][td]0.802[/td][/tr]
[tr][td]20[/td][td]Member[/td][td]utzmaster1[/td][td]Germany[/td][td]19[/td][td]8,202[/td][td]12,065[/td][td]0.68[/td][/tr]
[tr][td]21[/td][td]Member[/td][td]Jaynox[/td][td]Germany[/td][td]19[/td][td]1,711[/td][td]2,065[/td][td]0.829[/td][/tr]
[tr][td]22[/td][td]Member[/td][td]ETOJohnson[/td][td]Germany[/td][td]17[/td][td]3,707[/td][td]3,904[/td][td]0.95[/td][/tr]
[tr][td]23[/td][td]Member[/td][td]xphoenix7[/td][td]Germany[/td][td]16[/td][td]3,930[/td][td]3,067[/td][td]1.281[/td][/tr]
[tr][td]24[/td][td]Member[/td][td]Vegetox[/td][td]Germany[/td][td]13[/td][td]824[/td][td]3,224[/td][td]0.256[/td][/tr]
[tr][td]25[/td][td]Member[/td][td]Walr0ssMAnn[/td][td]Germany[/td][td]11[/td][td]1,851[/td][td]1,701[/td][td]1.088[/td][/tr]
[tr][td]26[/td][td]Member[/td][td]0xtime2f4mex0[/td][td]Germany[/td][td]2[/td][td]503[/td][td]310[/td][td]1.623[/td][/tr]
[tr][td]27[/td][td]Member[/td][td]Raiinbow7x[/td][td]Germany[/td][td]2[/td][td]93[/td][td]31[/td][td]3[/td][/tr]
[tr][td]28[/td][td]Member[/td][td]xL1MiTx[/td][td]Germany[/td][td]1[/td][td]3[/td][td]4[/td][td]0.75[/td][/tr]
[/table]

您从网站检索/解析数据的脚本由于保留了一些html标签而被破坏。这是一个工作版本(不包括bbcode标签tho)

#!/bin/bash
# Fetch current level of client from WA-STA.NET
WASTA_USER_URL="http://www.wa-sta.net/index.php?menu=clan&clanid=1091&get=members"
# Create temp files
TEMPFILE_1=$(mktemp /tmp/wasta_download_page-XXXXX)
TEMPFILE_2=$(mktemp /tmp/wasta_download_page-XXXXX)
TEMPFILE_4=$(mktemp /tmp/wasta_download_page-XXXXX)
# Download 'WA-STA.com Clanmember List'
wget $WASTA_USER_URL -q -O - > $TEMPFILE_1
# Get the clanlist
sed -n "/<tbody>/,/</table>/p" $TEMPFILE_1 | tr "n" " " | 
sed 's/(<tr>|</tr>)/n/g'  > $TEMPFILE_2
entries=()
while read line; do
    entries+=("$(echo $line | sed -e 's:</td>:</td>n:g' |  
                perl -ne 'print "$1" if /<td.*?>(.*?)</td>/ ' | 
                sed -r "s:<div class='iLevel-LevelIcon_L([0-9]+)'[^>]*>:\1:g" | 
                sed -r "s:<img.*title='([^']*)'[^>]*>:\1:g" | 
                sed -e 's:<[^><]*>::g' | sed -r 's:s+:|:g')")
done < $TEMPFILE_2

   printf "%sn" "${entries[@]}"
rm $TEMPFILE_1
rm $TEMPFILE_2
exit 0;

输出:

1|Leader|Syrocs|Germany|56|76,307|58,677|1.3|
2|Manager|Syroxs|Germany|55|69,472|64,729|1.073|
3|Manager|checker284|Germany|44|43,827|62,385|0.703|
4|Member|Jeynox|Germany|73|206,387|185,675|1.112|
5|Member|0xtime2rushx0|Germany|64|1,767|1,003|1.762|
6|Member|FireFox7x|Germany|59|60,913|42,815|1.423|
7|Member|xxl3aaamxx|Germany|52|29,408|25,937|1.134|
8|Member|moechtegern|Germany|46|34,986|36,034|0.971|
9|Member|Syroxz|Germany|45|16,371|21,166|0.773|
10|Member|xFameB0zZ|Germany|43|38,007|31,258|1.216|
11|Member|xDirtyCrash7x|Germany|40|37,959|24,787|1.531|
12|Member|michii1992|Austria|35|27,993|18,076|1.549|
13|Member|GymBeast|Germany|35|14,220|11,958|1.189|
14|Member|vanillaz55|Germany|35|24,575|18,936|1.298|
15|Member|xNiino|Germany|34|15,093|11,157|1.353|
16|Member|Lowglette88|Germany|30|1,472|1,473|0.999|
17|Member|stumpf7|Germany|29|18,463|19,397|0.952|
18|Member|xChAoSxSKiLLzzx|Switzerland|27|15,545|13,434|1.157|
19|Member|xeasybeatzzx|Germany|25|7,782|9,709|0.802|
20|Member|utzmaster1|Germany|19|8,202|12,065|0.68|
21|Member|Jaynox|Germany|19|1,711|2,065|0.829|
22|Member|ETOJohnson|Germany|17|3,707|3,904|0.95|
23|Member|xphoenix7|Germany|16|3,930|3,067|1.281|
24|Member|Vegetox|Germany|13|824|3,224|0.256|
25|Member|Walr0ssMAnn|Germany|11|1,851|1,701|1.088|
26|Member|0xtime2f4mex0|Germany|2|503|310|1.623|
27|Member|Raiinbow7x|Germany|2|93|31|3|
28|Member|xL1MiTx|Germany|1|3|4|0.75|

相关内容

  • 没有找到相关文章

最新更新