unix:如何连接 grep 中匹配的文件
我想连接名称不包含“_BASE_”的文件。我认为这会是......
ls | grep -v _BASE_ | cat > all.txt
猫的部分是我没有得到正确的。有人能给我一些关于这个的想法吗?
I want to concatenate the files whose name does not include "_BASE_". I thought it would be somewhere along the lines of ...
ls | grep -v _BASE_ | cat > all.txt
the cat part is what I am not getting right. Can anybody give me some idea about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
Try this
您可以使用
--ignore
选项通过ls
忽略某些文件,然后将它们放入文件中。您也可以在没有
xargs
的情况下做到这一点:UPD:
Dale Hagglund 注意到,像“Some File”这样的文件名将显示为两个文件名,“Some”和“File”。为了避免这种情况,当
WORD
可以是shell
或escape
时,您可以使用--quoting-style=WORD
选项。例如,如果
--quoting-style=shell
Some File 将打印为“Some File”,并将被解释为一个文件。另一个问题是输出文件可能与 ls 编辑的文件之一相同。我们也需要忽略它。
所以答案是:
You can ignore some files with
ls
using--ignore
option and then cat them into a file.Also you can do that without
xargs
:UPD:
Dale Hagglund noticed, that filename like "Some File" will appear as two filenames, "Some" and "File". To avoid that you can use
--quoting-style=WORD
option, whenWORD
can beshell
orescape
.For example, if
--quoting-style=shell
Some File will print as 'Some File' and will be interpreted as one file.Another problem is output file could the same of one of
ls
ed files. We need to ignore it too.So answer is:
如果你还想从子目录中获取文件,“find”是你的朋友:
它搜索当前目录及其子目录中的文件,它只查找文件(
-type f
),忽略与通配符匹配的文件模式*_BASE_*
,忽略all.txt
,并以与xargs
相同的方式执行cat
。If you want to get also files from subdirectories, `find' is your friend:
It searches files in the current directory and its subdirectories, it finds only files (
-type f
), ignores files matching to wildcard pattern*_BASE_*
, ignoresall.txt
, and executescat
in the same manner asxargs
would.