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 / 问题 / 784411
Accepted
user447274
user447274
Asked: 2024-10-03 03:41:38 +0800 CST2024-10-03 03:41:38 +0800 CST 2024-10-03 03:41:38 +0800 CST

如何一次处理 N 个项目序列

  • 772

我想在 Linux 系统上使用 bash 和 xargs 并行运行 N 次命令。命令如下:

for i in `seq 1 $(howmany.txt)`; do aprogramm --file file$i.dat ; done

该文件howmany.txt包含值100,并且ntimes.txt包含值5。

文件按顺序从 1 到 100file1存在file100,我想一次并行处理 5 个文件。如何使用 执行此操作xargs?

bash
  • 5 5 个回答
  • 255 Views

5 个回答

  • Voted
  1. terdon
    2024-10-03T06:25:52+08:002024-10-03T06:25:52+08:00

    您展示的内容存在很多问题。首先,$(howmany.txt)将尝试执行howmany.txt。如果这是一个可执行脚本,存储在您的 PATH 中,它会打印出一个数字,那么它可能会起作用,但如果它只是一个带有数字的文本文件,您可能想要$(cat howmany.txt)。其次,最好使用$( command )而不是`seq`命令(更清晰且允许嵌套命令)。

    也就是说,要做到这一点xargs,你需要类似的东西:

    seq 1 $(< howmany.txt) | xargs -I {} -n1 -P $(< ntimes.txt) aprogramm --file file{}.dat
    

    但实际上,这是GNU parallel的工作:

    parallel -j ntimes.txt aprogramm --file file{} < howmany.txt
    

    或者,如果您的所有文件都已命名fileN,并且您想要处理所有文件fileN(因此您实际上不需要howmany.txt),那么您可以执行以下操作:

    parallel -j ntimes.txt aprogramm --file {} ::: file*
    

    parallel它具有众多优点,其中之一xargs就是您可以在运行时更改数量howmany.txt,从而增加或减少正在运行的作业数量。

    • 6
  2. Jim L.
    2024-10-03T04:46:54+08:002024-10-03T04:46:54+08:00

    不要编写脚本来执行命令,而是编写一个回显命令的脚本:

    for i in `seq 1 $(howmany.txt)`
      do echo "aprogramm --file file$i.dat"
    done
    

    无论您使用哪个 shell,该输出的每一行都应该是有效的命令语法。

    一旦输出看起来不错,就将其通过管道传输到parallel(1),并选择-j5并行运行 5 个作业:

    for i in `seq 1 $(howmany.txt)`
      do echo "aprogramm --file file$i.dat"
    done | parallel -j5
    
    • 5
  3. Best Answer
    choroba
    2024-10-03T04:19:49+08:002024-10-03T04:19:49+08:00

    要批量处理文件,请使用xargs -n。如果每批处理一个文件,请使用 1:

    echo file*.dat | xargs -n1
    

    要并行处理,请使用-P。

    echo file*.dat | xargs -n1 -P$(< ntimes.txt) aprogramm --file
    
    • 4
  4. markp-fuso
    2024-10-03T05:37:28+08:002024-10-03T05:37:28+08:00

    假设:

    • 文件howmany.txt,并ntimes.txt保证包含一个整数,不包含任何其他内容
    • 目录中可能还有其他我们不想处理的文件(例如,简单文件find . -name 'file*.dat'可能会返回比所需更多的文件)

    为了演示目的:

    $ cat aprogram
    #!/bin/bash
    
    echo "input: $@"
    sleep 3
    
    $ cat howmany.txt
    10
    
    $ cat ntimes.txt
    4
    

    一个想法是使用 Jim 的方法来打印通过管道传输到 xargs 的文件名列表:

    read -r max   < howmany.txt
    read -r count < ntimes.txt
    
    for ((i=1; i<=max; i++))
    do
        echo "file$i.dat"
    done | xargs -r -n1 -P "$count" ./aprogram --file
    

    这将生成:

    input: --file file1.dat        # 4 invocations in quick succession
    input: --file file2.dat
    input: --file file3.dat
    input: --file file4.dat
                                   # 3 sec delay
    input: --file file5.dat        # 4 invocations in quick succession
    input: --file file6.dat
    input: --file file7.dat
    input: --file file8.dat
                                   # 3 sec delay
    input: --file file9.dat        # last 2 invocations in quick succession
    input: --file file10.dat
                                   # 3 sec delay
    
    • 4
  5. symcbean
    2024-10-03T18:11:32+08:002024-10-03T18:11:32+08:00

    我最近必须在不使用 xargs/parallel 的情况下解决这个问题……

    NUM_THREADS=5
    
    for i in $( sequence_generation ) ; do
        while [ "$NUM_THREADS" -le "$( jobs  | grep 'Running' | wc -l)" ]; do
            sleep 1
        done
        {
            slow_thing $i 
        } &
    done
    while [ 1 -le "$( jobs  | grep 'Running' | wc -l)" ]; do
       sleep 1
    done
    
    • 1

相关问题

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

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

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

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

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

Sidebar

Stats

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

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

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

    • 4 个回答
  • Marko Smith

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

    • 5 个回答
  • Marko Smith

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

    • 3 个回答
  • 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
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +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

热门标签

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