gra*_*fox 4 macos bash for-loop cat batch-processing
我正在研究一个数学项目,只是碰到了bash编程的砖墙.
目前,我有一个包含800个文本文件的目录,我想要做的就是运行一个循环到什么猫第80个文件(_01通过对_80)到一个新的文件,并保存到其他地方,那么接下来的80(_81至_160)文件等等
目录中的所有文件都列出如下:ath_01,ath_02,ath_03等.
有人可以帮忙吗?
到目前为止,我有:
#!/bin/bash
for file in /dir/*
do
echo ${file}
done
Run Code Online (Sandbox Code Playgroud)
这只是简单列出我的文件.我知道我需要以某种方式使用cat file1 file2> newfile.txt,但它让我与_01,_02等的数字扩展混淆.
如果我更改文件的名称以使用除下划线之外的其他内容,它会有帮助吗?喜欢ath.01等?
干杯,
既然你提前知道你有多少文件以及它们是如何编号的,那么可以更容易"展开循环",可以这么说,并使用复制粘贴和一些手动调整来编写一个脚本使用支撑扩展.
#!/bin/bash
cat ath_{001..080} > file1.txt
cat ath_{081..160} > file2.txt
cat ath_{161..240} > file3.txt
cat ath_{241..320} > file4.txt
cat ath_{321..400} > file5.txt
cat ath_{401..480} > file6.txt
cat ath_{481..560} > file7.txt
cat ath_{561..640} > file8.txt
cat ath_{641..720} > file9.txt
cat ath_{721..800} > file10.txt
Run Code Online (Sandbox Code Playgroud)
或者,使用嵌套的for循环和seq命令
N=800
B=80
for n in $( seq 1 $B $N ); do
for i in $( seq $n $((n+B - 1)) ); do
cat ath_$i
done > file$((n/B + 1)).txt
done
Run Code Online (Sandbox Code Playgroud)
外部循环将遍历1,81,161 n等.内部循环将迭代i1到80,然后是81到160等.内部循环的主体只是将内容转储i到标准输出的文件中,但是循环的聚合输出存储在文件1中,然后存储在2中,等等.