unix:如何连接 grep 中匹配的文件

发布于 2024-12-21 18:28:59 字数 144 浏览 2 评论 0原文

我想连接名称不包含“_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

药祭#氼 2024-12-28 18:28:59

试试这个

ls | grep -v _BASE_ | xargs cat > all.txt

Try this

ls | grep -v _BASE_ | xargs cat > all.txt
撩人痒 2024-12-28 18:28:59

您可以使用 --ignore 选项通过 ls 忽略某些文件,然后将它们放入文件中。

ls --ignore="*_BASE_*" | xargs cat > all.txt 

您也可以在没有 xargs 的情况下做到这一点:

cat $( ls --ignore="*_BASE_*" ) > all.txt

UPD
Dale Hagglund 注意到,像“Some File”这样的文件名将显示为两个文件名,“Some”和“File”。为了避免这种情况,当 WORD 可以是 shellescape 时,您可以使用 --quoting-style=WORD 选项。

例如,如果 --quoting-style=shell Some File 将打印为“Some File”,并将被解释为一个文件。

另一个问题是输出文件可能与 ls 编辑的文件之一相同。我们也需要忽略它。

所以答案是:

outputFile=a.txt; ls --ignore="*sh*" --ignore="${outputFile}" --quoting-style=shell | xargs cat > ${outputFile}

You can ignore some files with ls using --ignore option and then cat them into a file.

ls --ignore="*_BASE_*" | xargs cat > all.txt 

Also you can do that without xargs:

cat $( ls --ignore="*_BASE_*" ) > all.txt

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, when WORD can be shell or escape.

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 lsed files. We need to ignore it too.

So answer is:

outputFile=a.txt; ls --ignore="*sh*" --ignore="${outputFile}" --quoting-style=shell | xargs cat > ${outputFile}
深陷 2024-12-28 18:28:59

如果你还想从子目录中获取文件,“find”是你的朋友:

find . -type f ! -name '*_BASE_*' ! -path ./all.txt -exec cat {} >> all.txt \+

它搜索当前目录及其子目录中的文件,它只查找文件(-type f),忽略与通配符匹配的文件模式 *_BASE_*,忽略 all.txt,并以与 xargs 相同的方式执行 cat

If you want to get also files from subdirectories, `find' is your friend:

find . -type f ! -name '*_BASE_*' ! -path ./all.txt -exec cat {} >> all.txt \+

It searches files in the current directory and its subdirectories, it finds only files (-type f), ignores files matching to wildcard pattern *_BASE_*, ignores all.txt, and executes cat in the same manner as xargs would.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文