bash 如何从多个文件中读取数据行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9477792/
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
How to read lines of data from multiple files
提问by conandor
I need to extract data from files in directory /tmp/log.
I have no problem extract from single file.
我需要从目录中的文件中提取数据/tmp/log。我从单个文件中提取没有问题。
#!/bin/bash
while read line;
do
echo $line
done < /tmp/log/file1
I want try it with multiple files /tmp/log/*but it returned error ambiguous redirect.
Any idea how can I around it?
我想用多个文件尝试它,/tmp/log/*但它返回了错误ambiguous redirect。知道我该如何解决吗?
回答by jcollado
You could read the files in a for loop as follows:
您可以在 for 循环中读取文件,如下所示:
for file in /tmp/log/*; do
while read -r line; do
echo "$line"
done < "$file"
done
The strategy is just wrap your while loop with a for loop that takes care of processing each of the files one at a time.
该策略只是将您的 while 循环包裹在一个 for 循环中,该循环负责一次处理每个文件。
回答by Vijay
Dont know exactly waht you need.. probably you are looking for this:
不知道你到底需要什么......可能你正在寻找这个:
cat /tmp/log/*
Is this what you need?
这是你需要的吗?
for line in `cat /tmp/log/*`
do
echo $line
done

