Any Simple Way to Do for Loop Pipe, Such as ` Ls | Grep ". *.7Z" | 7Z -E {}`
I Am Curious That Is There an Function/Command to Cope for Loop Pipe? It Is Easy to Occur Below Demand You Run a Command Return Multiple Line You Need Process...
I am curious that is there an function/command to cope for loop pipe ?
It is easy to occur below demand
- you run a command return multiple line
- you need process each line
- pass each line to another function/command
For example:
you need find some path match a parttern , move each to another place :
# 1.
find path_A -name '*AAA*' -exec mv -t path_B {} +
# 2.
find path_A -name "*AAA*" -print0 | xargs -0 -I {} mv {} path_B
People would be confused,
- why need mv
-t(I mean, through a [for loop pipe] you can usemv {} some_path,just like what you wrote without pipe ) - why exec or | xargs is write in that way , xargs need much more options, but they are actually no need in many computer language 。
- why I can't use
ls ... mv -t path_B {} +, orls | xargs -0 -I {} mv {} path_B, in other words , why it doesn't work after I change to another command ? - Why I failed when change
mvto7z:find path_A -name '*AAA*' -exec 7z -t {} +?
NOTE: Above are not question, just examples!!
I asked this question for a common and simple way to cope for loop pipe . Is there a simple and common way to do:
# 0
command with option (usually you use a command like this)
# 1
command with option [for loop pipe] command with option | command with option
# 2
command with option [for loop pipe] command with option | command with option [for loop pipe] command with option
# 3
....
So you don't need change command with option when you use [for loop pipe] . This would be much convenient for daily use .
Update
I put all the example just want to say normal way is confusing when different comand has it own style to cope with multiline input, it would need exec or xargs or some else options . if we have a for pipe command which can connect two command with option . It would be much easy to use
I mean, if you are using an advanced language. For example python,
def pipe(x, func, *func_options):
return func(x, *func_options)
def forloop_pipe(x, func, *func_options):
return [func(i, *func_options) for i in x.split('\n')]
def ls(*options)
pass
def mv(*options)
pass
a = ls('*.txt')
b = forloop_pipe(a, mv, '/home')
This is a very simple example with a lot of flaws, but it explains what I'm trying to get in bash command .
3 Answers
I'm not sure this is a straight answer to the question (there are much more than one questions), but I think you are asking for something like:
forloop with command substitution orwhileloop with process substitution.
Must Read
for loop with $(command substitution)
Command substitution allows the output of a command to replace the command itself. We can combine this functionality with the for loop in this way:
for item in "$(find . -type f)"
do
echo "$item" | tee -a ./"file-list.txt"
done