循环内的AWK,用于使多个文件形成单个文件 [英] AWK inside a loop for making multiple files form single files

查看:150
本文介绍了循环内的AWK,用于使多个文件形成单个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下名为ABCD.vasp的文件:

# A B C D
1.000000
     13.85640621        0.00000000        0.00000000
      4.61880236       13.06394496        0.00000000
      0.00000000        0.00000000       45.25483322
A B C D
   32      32      32      32
Selective dynamics
Direct
      0.00000000        0.00000000        0.00000000 F F F
      0.00000000        0.00000000        0.12500000 F F T
      0.00000000        0.00000000        0.25000000 F F T
      0.00000000        0.00000000        0.37500000 F F T
      0.50000000        0.00000000        0.00000000 F F F
      0.50000000        0.00000000        0.12500000 F F T
      0.50000000        0.00000000        0.25000000 F F T
      0.50000000        0.00000000        0.37500000 F F T
      0.12500000        0.37500000        0.06250000 F F T
      0.12500000        0.37500000        0.18750000 F F T
      0.12500000        0.37500000        0.31250000 F F T  

我要对上述文件执行的操作:

The operations which I want to do on the above file:

  1. 我想将前9行复制到名为test.vasp
  2. 的新文件中
  3. 行号10开始,如果第三列是>=0.25,则第一列应增加0.025,并且整行应附加到test1.vasp
  4. 行号10起,如果第三列是<0.25,则应将整行附加到test1.vasp
  5. 我想从0.025 to 0.25重复test1.vasp to test10.vasp
  6. 的步骤2和步骤3
  1. I want to copy the first 9 lines into a new file named test.vasp
  2. Line number 10 onwards, if the third column is >=0.25 then the first column should be increased by 0.025 and the entire line should be appended to test1.vasp
  3. Line number 10 onwards, if the third column is <0.25 then the entire line should be appended to test1.vasp
  4. Step-2 and Step-3 I want to repeat from 0.025 to 0.25 for test1.vasp to test10.vasp

我尝试了以下不起作用的命令:

I tried the below command which is not working:

for ((i=1;i<=10;i++));do
        outputfile=test"$i".vasp
        awk -v I=$i 'NR > 9 && $3+0 >= 0.25 {
        p = $1; p =($I * 0.025); sub(/[^ \t]+/, sprintf("%.8f", p)) } 1' $inputfile > $outputfile
done

请在awksed

推荐答案

您可以使用此awk脚本.

cat incr.awk

NR <= 9 {
   s = s $0 ORS
   next
}
{
   r[++n] = $0
}
END {
   for (i=1; i<=10; i++) {
      fn = "test" i ".vasp"
      printf "%s", s > fn
      for (k=1; k<=n; ++k) {
         split(r[k], a)
         if (a[3] >= 0.25) {
            sub(/[^\t]+/, sprintf("%.8f", a[1] + .025), r[k])
         }
         print r[k] > fn
      }
      close(fn)
   }
}

然后以以下方式运行它:

Then run it as:

awk -f incr.awk file

这篇关于循环内的AWK,用于使多个文件形成单个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆