我在目录中有超过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++ )) doneI 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.txtIt 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.txtWhich adds each file to the ARGV array of files to process and then prints every line it sees.
更多推荐
发布评论