Running a command for each line of a text file (Bash)

Deadly 提交于 2020-01-03 02:46:33

问题


I am working on a program that does the following:

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

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; 

then

python file.py BBBB; 

then

python file.py CCCC. 

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

I tried the following:

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

text;

Thanks, guys!


回答1:


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



回答2:


This should work:

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


来源:https://stackoverflow.com/questions/29836900/running-a-command-for-each-line-of-a-text-file-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!