bash 手动迭代文件的一行 | 猛击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1456501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manually iterating a line of a file | bash
提问by Greg
I could do this in any other language, but with Bash I've looked far and wide and could not find the answer.
我可以用任何其他语言来做到这一点,但是使用 Bash 我已经四处寻找并且找不到答案。
I need to manually increase $linein a script. Example:
我需要$line在脚本中手动增加。例子:
for line in `cat file`
do
foo()
foo_loop(condition)
{
do_something_to_line($line)
}
done
If you notice, every time the foo_loopiterates, $linestays the same. I need to iterate $linethere, and make sure the original for loop only runs the number of lines in file.
如果您注意到,每次foo_loop迭代时,都$line保持不变。我需要在$line那里迭代,并确保原始 for 循环只运行文件中的行数。
I have thought about finding the number of lines in file using a different loop and iterating the line variable inside the inner loop of foo().
我想过使用不同的循环查找文件中的行数并在foo().
Any ideas?
有任何想法吗?
EDIT:
编辑:
Sorry for being so vague.
抱歉这么含糊。
Here we go:
开始了:
I'm trying to make a section of my code execute multiple times (parallel execution)
我试图使我的代码的一部分执行多次(并行执行)
Function foo() # Does something
for line in `cat $temp_file`;
foo($line)
That code works just fine, because foois just taking in the value of line; butif I wanted to do this:
该代码工作得很好,因为foo它只是接收 line 的值;但如果我想这样做:
Function foo() # Does something
for line in `cat $temp_file`;
while (some condition)
foo($line)
end
$linewill equal the same value throughout the while loop. I need it to change with the whileloop, then continue when it goes back to the for. Example:
$line在整个 while 循环中将等于相同的值。我需要它随着while循环改变,然后在它回到for. 例子:
line = Hi
foo{ echo "$line" };
for line in `cat file`;
while ( number_of_processes_running -lt max_number_allowed)
foo($line)
end
If the contents of file were
如果文件的内容是
Hi \n Bye \n Yellow \n Green \n
The output of the example program would be (if max number allowed was 3)
示例程序的输出将是(如果允许的最大数量为 3)
Hi Hi Hi Bye Bye Bye Yellow Yellow Yellow Green Green Green.
Where I want it to be
我想要的地方
Hi Bye Yellow Green
I hope this is better. I'm doing my best to explain my problem.
我希望这会更好。我正在尽力解释我的问题。
回答by Buggabill
Instead of using a for loop to read through the file you should maybe read through the file like so.
而不是使用 for 循环来读取文件,您应该像这样读取文件。
#!bin/bash
while read line
do
do_something_to_line($line)
done < "your.file"
回答by Steven
Long story short, while read line; do _____ ; done
长话短说, while read line; do _____ ; done
Then, make sure you have double-quotes around "$line" so that a parameter isn't delimited by spaces.
然后,确保在 "$line" 周围有双引号,以便参数不被空格分隔。
Example:
例子:
$ cat /proc/cpuinfo | md5sum
c2eb5696e59948852f66a82993016e5a *-
$ cat /proc/cpuinfo | while read line; do echo "$line"; done | md5sum
c2eb5696e59948852f66a82993016e5a *-
Second example # add .gz to every file in the current directory: # If any files had spaces, the mv command for that line would return an error.
第二个示例 # 将 .gz 添加到当前目录中的每个文件: # 如果任何文件有空格,则该行的 mv 命令将返回错误。
$ find -type f -maxdepth 1 | while read line; do mv "$line" "$line.gz"; done
回答by Paused until further notice.
You should post follow-ups as edits to your question or in comments rather than as an answer.
您应该将跟进作为对您的问题的编辑或在评论中而不是作为答案发布。
This structure:
这种结构:
while read line
do
for (( i=1; i<$max_number_allowed; i++ ))
do
foo $line
done
done < file
Yields:
产量:
Hi Hi Hi Bye Bye Bye ...etc.
While this one:
而这个:
for (( i=1; i<$max_number_allowed; i++ ))
do
while read line
do
foo $line
done < file
done
Yields:
产量:
Hi Bye Yellow Green Hi Bye Yellow Green ...etc.

