如何从 Bash 中的文件列表中捕获多个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11619500/
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 cat multiple files from a list of files in Bash?
提问by user1470511
I have a text file that holds a list of files. I want to cat
their contents together. What is the best way to do this? I was doing something like this but it seems overly complex:
我有一个包含文件列表的文本文件。我想把cat
他们的内容放在一起。做这个的最好方式是什么?我正在做这样的事情,但它似乎过于复杂:
let count=0
while read -r LINE
do
[[ "$line" =~ ^#.*$ ]] && continue
if [ $count -ne 0 ] ; then
file="$LINE"
while read PLINE
do
echo $PLINE | cat - myfilejs > /tmp/out && mv /tmp/out myfile.js
done < $file
fi
let count++
done < tmp
I was skipping commented lines and running into issues. There has to be a better way to do this, without two loops. Thanks!
我正在跳过注释行并遇到问题。必须有更好的方法来做到这一点,没有两个循环。谢谢!
回答by Bernhard
Or in a simple command
或者在一个简单的命令中
cat $(grep -v '^#' files) > output
回答by kojiro
#!/bin/bash
files=()
while read; do
case "$REPLY" in
\#*|'') continue;;
*) files+=( "$REPLY" );;
esac
done < input
cat "${files[@]}"
What's better about this approach is that:
这种方法的更好之处在于:
- The only external command,
cat
, only gets executed once. - It's pretty careful to maintain significant whitespace for any given line/filename.
- 唯一的外部命令
cat
仅执行一次。 - 为任何给定的行/文件名维护重要的空格是非常小心的。
回答by Ignacio Vazquez-Abrams
{
while read file
do
#process comments here with continue
cat "$file"
done
} < tmp > newfile
回答by Lars Kotthoff
How about cat $(cat listoffiles) | grep -v "^#"
?
怎么样cat $(cat listoffiles) | grep -v "^#"
?
回答by sh3rmy
I ended up with this:
我结束了这个:
sed '/^$/d;/^#/d;s/^/cat "/;s/$/";/' files | sh > output
Steps:
脚步:
sedremoves blank lines & commented out lines from files.
sedthen wraps each line in
cat "
and";
.Script is then piped to shwith and into output.
sed从文件中删除空行和注释掉的行。
sed然后将每一行包裹在
cat "
and 中";
。然后脚本通过管道传送到sh并进入output。