AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1289324
Accepted
Praveen
Praveen
Asked: 2020-11-04 05:27:16 +0800 CST2020-11-04 05:27:16 +0800 CST 2020-11-04 05:27:16 +0800 CST

从文本文件到另一个模式之间的命令行内容复制

  • 772

我将 html 文件内容复制到以下形式的文本文件中:

... course 10> user_1 </a><br /><a class="reviewlink" href="https://www.foo.com/moodle/mod/quiz/review.php?attempt=1491">Review attempt<...
... course 10> user_2 </a><br /><a class="reviewlink" href="https://www.foo.com/moodle/mod/quiz/review.php?attempt=1498">Review attempt<...

从这里使用一些shell命令我可以得到内容:

user_1 "https://www.foo.com/moodle/mod/quiz/review.php?attempt=1491"
user_2 "https://www.foo.com/moodle/mod/quiz/review.php?attempt=1498"

到另一个文本文件。

这里...表示一些大的独特文本。

编辑

为用户提供更详细的表格:

 <label for="attemptid_1502" class="accesshide">Select attempt</label></td><td class="cell c1 picture" id="mod-quiz-report-responses-report_r1_c1"><a href="https://www.foo.com/moodle/user/view.php?id=380&amp;course=10" class="d-inline-block aabtn"><img src="https://www.foo.com/moodle/pluginfile.php/419/user/icon/moove/f2?rev=6942" class="userpicture" width="35" height="35" alt="Picture of user 1" title="Picture of user 1" /></a></td><td class="cell c2 bold" id="mod-quiz-report-responses-report_r1_c2"><a href="https://www.foo.com/moodle/user/view.php?id=380&amp;course=10">user 1</a><br /><a class="reviewlink" href="https://www.foo.com/moodle/mod/quiz/review.php?attempt=1502">Review attempt</a></td><td class="cell c3" id="mod-quiz-report-responses-report_r1_c3">meethu</td><td class="cell c4" id="mod-quiz-report-responses-report_r1_c4">text</td><td class="cell c5" id="mod-quiz-report-responses-report_r1_c5">Finished</td><td class="cell c6 bold" id="mod-quiz-report-responses-report_r1_c6"><a href="review.php?q=62&amp;attempt=1502">0.00</a></td><td class="cell c7" id="mod-quiz-report-responses-report_r1_c7"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=1" id="action_link5fa01d6f2fd9a29" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c8" id="mod-quiz-report-responses-report_r1_c8"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=2" id="action_link5fa01d6f2fd9a30" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c9" id="mod-quiz-report-responses-report_r1_c9"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=3" id="action_link5fa01d6f2fd9a31" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c10" id="mod-quiz-report-responses-report_r1_c10"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=4" id="action_link5fa01d6f2fd9a32" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c11" id="mod-quiz-report-responses-report_r1_c11"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=5" id="action_link5fa01d6f2fd9a33" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c12" id="mod-quiz-report-responses-report_r1_c12"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=6" id="action_link5fa01d6f2fd9a34" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c13" id="mod-quiz-report-responses-report_r1_c13"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=7" id="action_link5fa01d6f2fd9a35" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c14" id="mod-quiz-report-responses-report_r1_c14"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=8" id="action_link5fa01d6f2fd9a36" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c15" id="mod-quiz-report-responses-report_r1_c15"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=9" id="action_link5fa01d6f2fd9a37" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c16" id="mod-quiz-report-responses-report_r1_c16"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=10" id="action_link5fa01d6f2fd9a38" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c17" id="mod-quiz-report-responses-report_r1_c17"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=11" id="action_link5fa01d6f2fd9a39" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c18" id="mod-quiz-report-responses-report_r1_c18"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=12" id="action_link5fa01d6f2fd9a40" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td><td class="cell c19" id="mod-quiz-report-responses-report_r1_c19"><a href="https://www.foo.com/moodle/mod/quiz/reviewquestion.php?attempt=1502&slot=13" id="action_link5fa01d6f2fd9a41" class=""  title="Review response" ><span class="que"><i class="icon fa fa-remove text-danger fa-fw icon"  title="Incorrect" aria-label="Incorrect"></i><span class="notanswered">-</span></span></a></td></tr><tr class="" id="mod-quiz-report-responses-report_r2"><td class="cell c0" id="mod-quiz-report-responses-report_r2_c0"><input id="attemptid_1505" name="attemptid[]" type="checkbox"  value="1505"
   data-action="toggle"
   data-toggle="slave"
   data-togglegroup="quiz-attempts"
   
/>
scripts command-line bash sed copy
  • 2 2 个回答
  • 50 Views

2 个回答

  • Voted
  1. Best Answer
    pLumo
    2020-11-04T06:04:31+08:002020-11-04T06:04:31+08:00

    将 python 与beautifulsoup4一起使用,使用pip.

    sudo apt install python-pip
    pip install bs4
    

    把它放在一个文件中script.py:

    #!/usr/bin/env python
    
    from bs4 import BeautifulSoup
    
    # Open your file and read+parse its contents with BeautifulSoup
    with open('file.html', 'r') as f:
    
        contents = f.read()
        soup = BeautifulSoup(contents, 'lxml')
      
        # Find and loop all <td class="c2"> elements
        for td in soup.findAll('td', {'class': 'c2'}):
            
            # Find the first <a> element below <td> and get its text value
            user_name = td.find("a", recursive=False).text
            
            # Find the <a class="reviewlink"> element and get its `href`
            review_href = td.find("a", {'class' : 'reviewlink'}, recursive=False).get('href')
    
            # Print the information as you wish
            print('{} "{}"'.format(user_name, review_href))
    

    然后运行python script.py

    输出:

    user 1 "https://www.foo.com/moodle/mod/quiz/review.php?attempt=1502"
    
    • 1
  2. user986805
    2020-11-05T11:05:34+08:002020-11-05T11:05:34+08:00

    XMLStarlet 是一组用于操作 XML 文档的工具,在使用 XPath 查询元素时非常有用。

    $ xmlstarlet -q fo --html file.html | \
      xmlstarlet sel -t -m '//td[contains(@class,"c2")]' \
        -v 'a[1]/text()' -o \ \" -v 'a[2]/@href' -o \" -n 2> /dev/null
    

    如果你有一个格式良好的文档,你可以跳过第一行。为了查询元素,XMLStarlet 使用模板。一个模板可以包含多个子选项。在这种情况下,我们首先-m匹配td包含链接的元素。然后结果将传递给下一个子选项,我们在其中打印链接的值。

    • 0

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve