从Shell中的目录连接大量选择性文件(Concatenation of huge number of selective files from a directory in Shell)

编程入门 行业动态 更新时间:2024-10-27 08:28:46
从Shell中的目录连接大量选择性文件(Concatenation of huge number of selective files from a directory in Shell)

我在目录中有超过50000个文件,例如file1.txt,file2.txt,.....,file50000.txt。 我想连接一些文件的文件号列在下面的文本文件(need.txt)中。

need.txt 1 4 35 45 71 . . .

我尝试了以下内容。 虽然它有效,但我寻找更简单和简短的方法。

n1=1 n2=$(wc -l < need.txt) while [ $n1 -le $n2 ] do f1=$(awk 'NR=="$n1" {print $1}' need.txt) cat file$f1.txt >> out.txt (( n1++ )) done

I have more than 50000 files in a directory such as file1.txt, file2.txt, ....., file50000.txt. I would like to concatenate of some files whose file numbers are listed in the following text file (need.txt).

need.txt 1 4 35 45 71 . . .

I tried with the following. Though it is working, but I look for more simpler and short way.

n1=1 n2=$(wc -l < need.txt) while [ $n1 -le $n2 ] do f1=$(awk 'NR=="$n1" {print $1}' need.txt) cat file$f1.txt >> out.txt (( n1++ )) done

最满意答案

这样的事情对你有用:

sed -e 's/.*/file&.txt/' need.txt | xargs cat > out.txt

它使用sed将每一行转换为适当的文件名,然后将文件名交给xargs将它们交给cat 。

使用awk可以这样做:

awk 'NR==FNR{ARGV[ARGC]="file"$1".txt"; ARGC++; next} {print}' need.txt > out.txt

这会将每个文件添加到要处理的ARGV文件数组中,然后打印它看到的每一行。

Something like this should work for you:

sed -e 's/.*/file&.txt/' need.txt | xargs cat > out.txt

It uses sed to translate each line into the appropriate file name and then hands the filenames to xargs to hand them to cat.

Using awk it could be done this way:

awk 'NR==FNR{ARGV[ARGC]="file"$1".txt"; ARGC++; next} {print}' need.txt > out.txt

Which adds each file to the ARGV array of files to process and then prints every line it sees.

更多推荐

本文发布于:2023-07-25 10:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1260066.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选择性   文件   目录   Concatenation   Shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!