如何使用AWK编辑特定行并照原样复制其余部分? [英] How to edit specific lines using AWK and copy rest as it is?

查看:47
本文介绍了如何使用AWK编辑特定行并照原样复制其余部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the below file named 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 该行应附加到 test.vasp
  4. 行号 10 开始,如果第三列是< 0.25 ,则应将整行附加到 test.vasp
  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 test.vasp
  3. Line number 10 onwards, if the third column is <0.25 then the entire line should be appended to test.vasp

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

I tried the below command which is not working:

awk 'NR>=10&&NR<=137{if($3>=0.25);print $1+0.025,$2,$3,$4,$5,$6;next}
$3!=0.25{print $1,$2,$3,$4,$5,$6;next}}' ABCD.vasp > test.vasp

请在 awk sed

推荐答案

您可以使用以下 awk :

awk 'NR > 9 && $3+0 >= 0.25 { $1 += .025 } 1' file > test.vasp

请注意,在此过程中,前导空格和列之间的多个空格可能会被截断.如果您想保留这些,请使用以下 awk :

Note that leading whitespaces and multiple whitespaces between columns might get truncated in this process. If you want to keep those then use this awk:

awk 'NR > 9 && $3+0 >= 0.25 {
   p = $1; p += .025; sub(/[^ \t]+/, sprintf("%.8f", p)) } 1' file > test.vasp

cat test.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.02500000        0.00000000        0.25000000 F F T
      0.02500000        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.52500000        0.00000000        0.25000000 F F T
      0.52500000        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.15000000        0.37500000        0.31250000 F F T

这篇关于如何使用AWK编辑特定行并照原样复制其余部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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