在awk中使用循环变量循环ZSH shell脚本
等等 我想打印一系列文件在awk中使用循环变量循环ZSH shell脚本,shell,awk,zsh,Shell,Awk,Zsh,等等 我想打印一系列文件 last.xyz contains the following list of co-ordinates C 0.1 0.1 0.2 H 0.2 0.2 0.3 N 0.3 0.4 0.5 C 1.5 2.2 3.4 以此类推,我的第二列的增量为0.1、0.2、0.3,以此类推 这是我试过的代码 2ndmoleculeShift0.1.xyz 2ndmoleculeShift0.2.xyz 2ndmoleculeShift0.3.xyz 然而,它失败了。它给出
last.xyz contains the following list of co-ordinates
C 0.1 0.1 0.2
H 0.2 0.2 0.3
N 0.3 0.4 0.5
C 1.5 2.2 3.4
以此类推,我的第二列的增量为0.1、0.2、0.3,以此类推
这是我试过的代码
2ndmoleculeShift0.1.xyz
2ndmoleculeShift0.2.xyz
2ndmoleculeShift0.3.xyz
然而,它失败了。它给出了一个文件名为“2ndmoleculeShift0.1..10.xyz”的输出
这个代码有什么问题
我的脚本基本上应该执行以下操作
#!/bin/zsh
for i in {'0.1'..'10'}
do
awk '{print $1 " "$2+$i " " $3 " " $4}' last.xyz > '2ndmoleculeShift'$i'.xyz'
done
所需的输出是
for i in 0.1 to 10
awk '{print $1 " " $2+$i " "$3 " "$4}' last.xyz > 2ndmoleculeShift$i.xyz
等等我仍然不清楚,但我认为你想做的是这样的:
2ndmoleculeShift0.1.xyz
C 0.2 0.1 0.2
H 0.3 0.2 0.3
N 0.4 0.4 0.5
C 1.6 2.2 3.4
2ndmoleculeShift0.2.xyz
C 0.3 0.1 0.2
H 0.4 0.2 0.3
N 0.5 0.4 0.5
C 1.7 2.2 3.4
awk'
开始{ARGV[ARGC]=ARGV[ARGC-1];ARGC++}
NR==FNR{a[NR%58]=$0;numLines++;next}
FNR==(numLines-58){
对于(i=0.1;i(“移位的“i”.xyz”)
}
}
FNR>(numLines-58){
对于(i=0.1;i(“2ndmoleculeShift“i”.xyz”)
打印str>(“移位的“i”.xyz”)
}
}
““PC-PC.xyz”
@EdMorton我更新了question@EdMorton我简化了问题和最后一篇文章的内容。xyz@EdMorton更新了问题中错误的解释,并添加了所需的输出文件内容。如您更改了要求,并删除了“移位”输出文件和原始的“PC-PC.xyz”在我发布答案后输入文件。要记住的是,每当你编写一个shell循环只是为了操纵文本时,你的方法都是错误的。发明shell的人也发明了awk,让shell调用来进行文本处理,所以只需调用awk。@EdMorton point Noted此脚本非常有效。我试图做的是相似的
awk '
BEGIN{ ARGV[ARGC] = ARGV[ARGC-1]; ARGC++ }
NR == FNR { a[NR%58] = $0; numLines++; next }
FNR == (numLines-58) {
for (i=0.1; i<=10; i+=0.1) {
print a[FNR%58] > ("shifted" i ".xyz")
}
}
FNR > (numLines-58) {
for (i=0.1; i<=10; i+=0.1) {
str = $1 " " $2+i " " $3 " " $4
print str > ("2ndmoleculeShift" i ".xyz")
print str > ("shifted" i ".xyz")
}
}
' "PC-PC.xyz"