我正在运行一个作业,其中有多个文件夹(标记为 0 到 41),每个文件夹都包含一个相同的脚本,需要编辑第九行:
#SBATCH -J aXau19
X 应对应于文件夹的名称(0,1,2...等)
我试图弄清楚如何编写 bash 脚本来自动执行此操作,而不是手动更改它们。
我对 Linux 很陌生,但这是我想做的事情的总体思路:
for d in */; do;
#open script.sh in vi
# move down eight, across 12
#go into insert mode
#delete x
#take the directory name (just the number, not the full path)
#insert the directory name in place of x
#save and exit
done
任何指示将不胜感激!
答案1
这是ed(1)
一个 shellfor loop
解决方案。
Ed 应更改第九行并替换X
为目录名称。
我创建了一个小场景只是为了展示它是如何完成的。
在/tmp 内创建一个目录testing 以及空目录0 到41
mkdir -p /tmp/testing/{0..41}
检查目录
ls /tmp/testing
0/ 1/ 10/ 11/ 12/ 13/ 14/ 15/ 16/ 17/ 18/ 19/ 2/ 20/ 21/ 22/ 23/ 24/ 25/ 26/ 27/ 28/ 29/ 3/ 30/ 31/ 32/ 33/ 34/ 35/ 36/ 37/ 38/ 39/ 4/ 40/ 41/ 5/ 6/ 7/ 8/ 9/
创建一些文件并写入一些内容。
for d in /tmp/testing/*/; do
printf '%s\n' {1..20}'#SBATCH -J aXau19' > "$d"/myscript$RANDOM$RANDOM$RANDOM.sh
done
检查新创建的文件。
ls /tmp/testing/*
/tmp/testing/0:
myscript11901287773897.sh
/tmp/testing/1:
myscript22167100654321.sh
/tmp/testing/10:
myscript267471903230794.sh
/tmp/testing/11:
myscript21767360022483.sh
/tmp/testing/12:
myscript282132941319693.sh
/tmp/testing/13:
myscript6970151065611.sh
/tmp/testing/14:
myscript11068731012744.sh
/tmp/testing/15:
myscript19107175771.sh
/tmp/testing/16:
myscript12252298818021.sh
/tmp/testing/17:
myscript105202172129231.sh
/tmp/testing/18:
myscript2567385425240.sh
/tmp/testing/19:
myscript29570230212538.sh
/tmp/testing/2:
myscript11241700021570.sh
/tmp/testing/20:
myscript31918599925170.sh
/tmp/testing/21:
myscript21633144421815.sh
/tmp/testing/22:
myscript855984144154.sh
/tmp/testing/23:
myscript141701948311464.sh
/tmp/testing/24:
myscript26914582213221.sh
/tmp/testing/25:
myscript269851807416209.sh
/tmp/testing/26:
myscript12238285945162.sh
/tmp/testing/27:
myscript8701215006016.sh
/tmp/testing/28:
myscript11741830229037.sh
/tmp/testing/29:
myscript37121942613269.sh
/tmp/testing/3:
myscript252821857619268.sh
/tmp/testing/30:
myscript17684744220712.sh
/tmp/testing/31:
myscript67311600129127.sh
/tmp/testing/32:
myscript108853017115842.sh
/tmp/testing/33:
myscript223492431721664.sh
/tmp/testing/34:
myscript2802185356971.sh
/tmp/testing/35:
myscript19012307732797.sh
/tmp/testing/36:
myscript24174670724297.sh
/tmp/testing/37:
myscript3019178819832.sh
/tmp/testing/38:
myscript264601159329258.sh
/tmp/testing/39:
myscript6962410715067.sh
/tmp/testing/4:
myscript262302179122509.sh
/tmp/testing/40:
myscript14175285225742.sh
/tmp/testing/41:
myscript1053466403145.sh
/tmp/testing/5:
myscript263762898927462.sh
/tmp/testing/6:
myscript152723179213229.sh
/tmp/testing/7:
myscript222431803611235.sh
/tmp/testing/8:
myscript25041944217942.sh
/tmp/testing/9:
myscript16570686525823.sh
检查文件的内容。
tail -n+1 /tmp/testing/*/*.sh
每个文件包含以下内容
1#SBATCH -J aXau19
2#SBATCH -J aXau19
3#SBATCH -J aXau19
4#SBATCH -J aXau19
5#SBATCH -J aXau19
6#SBATCH -J aXau19
7#SBATCH -J aXau19
8#SBATCH -J aXau19
9#SBATCH -J aXau19
10#SBATCH -J aXau19
11#SBATCH -J aXau19
12#SBATCH -J aXau19
13#SBATCH -J aXau19
14#SBATCH -J aXau19
15#SBATCH -J aXau19
16#SBATCH -J aXau19
17#SBATCH -J aXau19
18#SBATCH -J aXau19
19#SBATCH -J aXau19
20#SBATCH -J aXau19
现在编辑文件。
for d in /tmp/testing/*/*.sh; do
printf '%s\n' "9,s/X/${d##*/}/" w | ed -s "$d"
done
检查已编辑的文件的内容ed(1)
tail -n+1 /tmp/testing/*/*.sh
如果我理解你的问题,那就是你想要做的,减去 vi/vim 的要求。