为文本文件的每一行运行命令(Bash) [英] Running a command for each line of a text file (Bash)

查看:49
本文介绍了为文本文件的每一行运行命令(Bash)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个执行以下操作的程序:

I am working on a program that does the following:

读取文件夹X中的270文件,并调用另一个程序以运行带有文件内容的命令.例如:

Reads 270 file in the folder X and calls another program to run a command with the contents of the file. For example:

$ cat X/1.txt
AAAA
BBBB
CCCC

该程序应该读取该文本(其中有270个),并在循环中应该这样做:

The program is supposed to read that text (there are 270 of them) and in the loop it's supposed to do:

$python file.py AAAA; 

然后

python file.py BBBB; 

然后

python file.py CCCC. 

以及下一个文件(如22.txt)的移动(这些文件不是数字,所以计数器不会有太大帮助).

and the move on the next file like 22.txt (the files aren't numeric so a counter wouldn't be much help).

我尝试了以下操作:

function text(){
for i in $(ls Domains/);
while read line;
do
        $line
        f= done < Domains/$i.txt
        python file.py $f
}

text;

谢谢,伙计们!

推荐答案

这是 xargs 派上用场的地方:将所有文件连接起来,并通过管道将其传递到xargs中,从而将每一行馈送到python程序中一个.

This is where xargs comes in handy: Concatenate all the files and pipe that into xargs which feeds each line to the python program one by one.

cat * | xargs -L1 python file.py

这篇关于为文本文件的每一行运行命令(Bash)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆