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
    • 最新
    • 标签
主页 / unix / 问题 / 435091
Accepted
Matt Zabojnik
Matt Zabojnik
Asked: 2018-04-03 09:25:39 +0800 CST2018-04-03 09:25:39 +0800 CST 2018-04-03 09:25:39 +0800 CST

如何从具有多行的列表中获取一个文件名?

  • 772

我想获取我从 XML 文件中检索的项目列表。我正在使用 sed 清理 XML,最终得到如下输出:

CountofMonteCristo.zip
English.
http://www.archive.org/download/count_monte_cristo_0711_librivox/count_monte_cristo_0711_librivox_64kb_mp3.zip
Alexandre.
Dumas.
LettersofTwoBrides.zip
English.
http://www.archive.org/download/letters_brides_0709_librivox/letters_brides_0709_librivox_64kb_mp3.zip
Honoréde.
Balzac.
BleakHouse.zip
English.
http://www.archive.org/download/bleak_house_cl_librivox/bleak_house_cl_librivox_64kb_mp3.zip
Charles.
Dickens.

我想使用 wget -i 将这些文件下载为 Language.Lastname.Firstname.Title.zip

我愿意以某种方式重新排列文件,以便我可以使用 $filename $url

我尝试了一些不同的 sed 命令。Sed 是我用来清理 XML 标记的工具,但我不知道如何将文本移动到适当的位置。每个文件的标题、名称和语言会有所不同。

编辑:在使用 sed 清理标签之前,每一行都包含在标签中,例如 English 和 FileTitle。我认为这可能有助于识别模式以重新安排事物。

EDIT2:这是XML 源代码

EDIT3:像这样的东西看起来会起作用,但我无法修改它以满足我的需要。

我的最终目标是将所有文件组织到文件夹中,其层次结构为 Language -> AuthorLastnameFirstname -> Files.zip

如果我所做的不是最佳实践,我愿意接受其他方法。

谢谢

bash wget
  • 3 3 个回答
  • 654 Views

3 个回答

  • Voted
  1. Jamie Lindsey
    2018-04-03T15:15:00+08:002018-04-03T15:15:00+08:00

    如果我所做的不是最佳实践,我愿意接受其他方法。

    我会建议你不要使用bash或sed等!并采用python方式,这绝对是一种更好的方式来解析你需要解析的xml。我刚刚用 python3.6 编写并测试了它,它完全符合您的要求。

    #!/usr/bin/python3
    # Let's import the modules we need
    import wget
    import os
    import requests
    from bs4 import BeautifulSoup as bs
    
    # Assign the url to a variable (not essential as we 
    # only use it once, but it's pythonic)
    url = 'https://librivox.org/api/feed/audiobooks/?offset=0&limit=3&fields=%7Blanguage,authors,title,url_zip_file%7B'
    
    # Use requests to fetch the raw xml
    r = requests.get(url)
    
    # Use BeautifulSoup and lxml to parse the raw xml so 
    # we can do stuff with it
    s = bs(r.text, 'lxml')
    
    # We need to find the data we need. This will find it and create some 
    # python lists for us to loop through later
    
    # Find all xml tags named 'url_zip_file' and assign them to variable
    links = s.find_all('url_zip_file')
    
    # Find all xml tags named 'last_name' and assign them to variable
    last_names = s.find_all('last_name')
    
    # Find all xml tags named 'last_name' and assign them to variable
    first_names = s.find_all('first_name')
    
    # Find all xml tags named 'language' and assign them to variable
    language = s.find_all('language')
    
    # Assign the language to a variable
    english = language[0].text
    
    # Make our new language directory
    os.mkdir(english)
    
    # cd into our new language directory
    os.chdir(str(english))
    
    # Loop through the last names (ln), first names(fn) and links
    # so we can make the directories, download the file, rename the 
    # file then we go back a directory and loop again
    for ln, fn, link in zip(last_names, first_names, links):
        os.mkdir('Author{}{}'.format(str(ln.text), str(fn.text)))
        os.chdir('Author{}{}'.format(ln.text, fn.text))
        filename = wget.download(link.text)
        os.rename(filename, 'File.zip')
        os.chdir('../')
    

    您可以将其保存到文件中,也可以将其粘贴/键入到 python3 解释器 cli 中,这取决于您。

    您需要使用 pip 或 easy_install 等安装python3-wget和beautifulsoup4 。

    • 1
  2. muru
    2018-04-03T21:03:11+08:002018-04-03T21:03:11+08:00

    如果可以使用,Librivox API 还提供 JSON 输出,使用适当的 XML 工具jq解析 JSON 可能比解析 XML 更容易。jq

    u='https://librivox.org/api/feed/audiobooks/?offset=0&limit=3&fields=%7Blanguage,authors,title,url_zip_file%7B&format=json'
    curl "$u" -sL |
      jq -r '.books[] | "\(.language).\(.authors[0].last_name + .authors[0].first_name).\(.title).zip", .url_zip_file'
    

    给出如下输出:

    English.DumasAlexandre.Count of Monte Cristo.zip
    http://www.archive.org/download/count_monte_cristo_0711_librivox/count_monte_cristo_0711_librivox_64kb_mp3.zip
    English.BalzacHonoré de.Letters of Two Brides.zip
    http://www.archive.org/download/letters_brides_0709_librivox/letters_brides_0709_librivox_64kb_mp3.zip
    English.DickensCharles.Bleak House.zip
    http://www.archive.org/download/bleak_house_cl_librivox/bleak_house_cl_librivox_64kb_mp3.zip
    

    之后,使用起来相对简单xargs:

    curl "$u" -sL |
      jq -r '.books[] | "\(.language).\(.authors[0].last_name + .authors[0].first_name).\(.title).zip", .url_zip_file' |
      xargs -d '\n' -n2 wget -O
    

    其中xargs使用两行作为参数wget,第一行成为-O选项参数,第二行成为 URL。

    虽然我会推荐像 Jamie's 这样的基于 Python 的解决方案,除了使用 JSON 和 Python 的内置 JSON 功能而不是 bs4。

    • 1
  3. Best Answer
    bu5hman
    2018-04-03T13:06:00+08:002018-04-03T13:06:00+08:00

    蛮力。

    如果您解析的 xml 在books

    while read a; read b; read c; read d; read e; do wget $c -O $b$e$d$a; echo $c; done < books
    

    只需将您的行重新组合为变量,只要您的记录块填充到 5 行,您就可以继续使用。

    • 0

相关问题

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

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

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    ssh 无法协商:“找不到匹配的密码”,正在拒绝 cbc

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    如何卸载内核模块“nvidia-drm”?

    • 13 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Wong Jia Hau ssh-add 返回:“连接代理时出错:没有这样的文件或目录” 2018-08-24 23:28:13 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST
  • Martin Hope
    Bagas Sanjaya 为什么 Linux 使用 LF 作为换行符? 2017-12-20 05:48:21 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

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

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve